如何在datalist中使用findcontrol! [英] How to use findcontrol in datalist !

查看:82
本文介绍了如何在datalist中使用findcontrol!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的后续代码隐藏中,我想使用datalist1将值分配给Label4。之前,在'datalist1'中,我将值从'item_name'列传递给'Label1'。在button18_click

我得到以下异常:'对象引用未设置为对象的实例'。

任何人都可以指导我这方面以便我可以通过代码背后的Label4值。

asp.net是否有这个功能可以在datalist中查找lyes?



In my following code-behind I want to assign the value to Label4 using datalist1. Earlier, in 'datalist1' I pass value to 'Label1' from column 'item_name'. On button18_click
I get the following exception as: 'Object reference not set to an instance of an object'.
Can anyone guide me in this regard so that I could pass the value to Label4 in code behind.
Does asp.net has this facility to findcontrol that lyes within a datalist?

<asp:DataList ID="Datalist1" runat="server" DataKeyField="item_code"  RepeatDirection="Horizontal"

    RepeatLayout="Table"  OnItemCommand="dl_item_command"  >
    <HeaderTemplate>
    </HeaderTemplate>
    <ItemTemplate>
    Item Name:
    <asp:Label Id="Itmnamelbl" runat="server" Text='<%#Eval("item_name") %>'>
    </asp:Label>
    <br />
<asp:Button ID="Button18" runat="server" ForeColor="White"  onclick="Button18_Click"

 BackColor="Teal" Font-Size="small"

 style="z-index: 1;font-family:Sans-Serif; height:22px;  font-weight:900; "

 Text="Add to cart" />
 </ItemTemplate>
<SelectedItemTemplate>
<asp:Label id="Label1"

Text='<%#DataBinder.Eval(Container.DataItem,"item_name") %>'

runat="server"/>
<br />
</SelectedItemTemplate>
</asp:DataList>




protected void Button18_Click(object sender, EventArgs e)
    {
              Label4.Text = (Datalist1.SelectedItem.FindControl("Label1").ToString());
    }

推荐答案



Hi,
If you have set OnItemCommand event of datalist, then why you are firing alone button click event, which is inside datalist??
Add commandname,commandargument to button and handle button click event in OnItemCommand of datalist. 




<!--design side-->
<asp:datalist id="Datalist1" runat="server" datakeyfield="item_code" repeatdirection="Horizontal" repeatlayout="Table" onitemcommand="dl_item_command" xmlns:asp="#unknown">
    <headertemplate>
    </headertemplate>
    <itemtemplate>
         .....
         <!--See CommandName. Add CommandArgument if you required to do so.-->
         <asp:button id="Button18" runat="server" forecolor="White" commandname="myevent">
         BackColor="Teal" Font-Size="small" style="z-index: 1;font-family:Sans-Serif;
         height:22px;  font-weight:900;" Text="Add to cart" />
    </asp:button></itemtemplate>
</asp:datalist>




//Server side
protected void dl_item_command(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "myevent") // check commandname here
    {
        int index = e.Item.ItemIndex;
        Label lbl = (Label)Datalist1.Items[index].FindControl("Label1");
        Label4.Text = lbl.Text; 
        // your code
    } 
}



希望对您有所帮助。

谢谢。


Hope it helps you.
Thanks.


您忘记将对象类型转换为标签。

试试这个:

You forgot convert object type as label.
Try this :
Label4.Text = ((Label)DataList1.SelectedItem.FindControl("Label1")).Text;







- Amit


这篇关于如何在datalist中使用findcontrol!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆