项目数据绑定时的查找链接按钮

本文关键字:查找 链接 按钮 定时 项目数 数据 绑定 项目 | 更新日期: 2023-09-27 18:27:31

我有一个列表视图,代码如下:-

 <asp:ListView ID="ListView1" runat="server" 
        onitemcommand="ListView1_ItemCommand" 
        onitemdatabound="ListView1_ItemDataBound"
        DataKeyNames="Question_Id" onitemdeleting="ListView1_ItemDeleting" 
        onitemediting="ListView1_ItemEditing" 
            onpagepropertieschanging="ListView1_PagePropertiesChanging" 
            >
    <ItemTemplate>
    <ul>
    <li> <%# Eval("Questiontitle") %> 
     <%# Eval("Mainquestion")%> 
        <asp:LinkButton ID="LinkButton1" runat="server" CommandName="approve" CommandArgument='<%# Eval("Question_Id") %>'>Approve</asp:LinkButton> 
        <asp:LinkButton ID="LinkButton2" runat="server" CommandName="deny" CommandArgument='<%# Eval("Question_Id") %>'>Deny</asp:LinkButton> </li>
    </ul>
    </ItemTemplate>
    <EmptyDataTemplate>
            <br />No data found
    </EmptyDataTemplate>
    <LayoutTemplate>
        <div ID="itemPlaceholderContainer" runat="server" style="font-family: Verdana, Arial, Helvetica, sans-serif;">
        <span ID="itemPlaceholder" runat="server" />
        </div>
        <div style="text-align: center;background-color: #CCCCCC;font-family: Verdana,      Arial, Helvetica, sans-serif;color: #000000;">
        </div>    
        <asp:DataPager ID="dpListView" runat="server" PageSize="2" OnPreRender="ListView1_PreRender">
            <Fields>
                <asp:NumericPagerField ButtonType="Link"/>
            </Fields>
        </asp:DataPager>        
     </LayoutTemplate>
    </asp:ListView>

现在我的cs代码是

var cat = DropDownList1.SelectedValue;
        var DBL = Getdata.GetDataFromSP("Spgetquestionbycategoryid", new object[,] { { "cat" }, { cat } });
        if (DBL.Rows[e.Item.DataItemIndex]["Approval"].ToString() == "True")
        {
            LinkButton btn = ListView1.Controls[e.Item.DataItemIndex].FindControl("LinkButton1") as LinkButton;
            LinkButton btn1 = ListView1.Controls[e.Item.DataItemIndex].FindControl("LinkButton2") as LinkButton;
            btn.BackColor = System.Drawing.Color.Green;
            btn1.BackColor = System.Drawing.Color.Transparent;
        }
        else if (DBL.Rows[e.Item.DataItemIndex]["Approval"].ToString() == "")
        {
            LinkButton btn = ListView1.Controls[e.Item.DataItemIndex].FindControl("LinkButton2") as LinkButton;
            LinkButton btn1 = ListView1.Controls[e.Item.DataItemIndex].FindControl("LinkButton1") as LinkButton;
            btn.BackColor = System.Drawing.Color.Transparent;
            btn1.BackColor = System.Drawing.Color.Transparent;
        }
        else if (DBL.Rows[e.Item.DataItemIndex]["Approval"].ToString() == "False")
        {
            LinkButton btn = ListView1.Controls[e.Item.DataItemIndex].FindControl("LinkButton2") as LinkButton;
            LinkButton btn1 = ListView1.Controls[e.Item.DataItemIndex].FindControl("LinkButton1") as LinkButton;
            btn.BackColor = System.Drawing.Color.Red;
            btn1.BackColor = System.Drawing.Color.Transparent;
        }

现在,在这个地方,当我试图找到我的链接按钮时,它给了我空对象引用错误。因为我有一些条件要应用到链接按钮。

如果我从中删除数据分页。我没有收到任何错误,它运行得很好。请提出建议。

项目数据绑定时的查找链接按钮

您应该在PagePropertiesChanged事件中再次DataBind您的ListView:

protected void ListView_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
   ListView1.DataSource = getdataSource();
   ListView1.DataBind();
}
受保护的void ListView1_ItemDataBound(对象发送方,ListViewItemEventArgs e){if(e.Item.ItemType==ListViewItemType.DataItem){
        LinkButton btn1 = (LinkButton)e.Item.FindControl("LinkButton1");
        var approveDeny = false;
        ListViewDataItem dataItem = (ListViewDataItem)e.Item;
        System.Boolean.TryParse(DataBinder.Eval(dataItem.DataItem, "ApproveOrDeny").ToString(), out approveDeny);
        if (approveDeny)
        {
            btn1.BackColor = System.Drawing.Color.Green;
            btn1.BackColor = System.Drawing.Color.Transparent;
        }
        else if (!approveDeny)
        {
            btn1.BackColor = System.Drawing.Color.Blue;
            btn1.BackColor = System.Drawing.Color.Transparent;
        }
        else
        {
            btn1.BackColor = System.Drawing.Color.Red;
            btn1.BackColor = System.Drawing.Color.Transparent;
        }
    }
}