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

时间:2012-03-13 10:25:00

标签: c# asp.net listview

我的列表视图包含以下代码: -

 <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;
        }

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

如果我从同一个数据中删除数据页面。我没有得到任何错误,它运行正常。 请建议。

2 个答案:

答案 0 :(得分:0)

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

protected void ListView_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
   ListView1.DataSource = getdataSource();
   ListView1.DataBind();
}

答案 1 :(得分:0)

protected void ListView1_ItemDataBound(object sender,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;
        }
    }
}