日期:2014-05-17  浏览次数:20528 次

DataList取某个单元格的值没取到
点击某个单元格里的按钮,然后显示的是最后一个单元格的值,这个是怎么回事?
HTML code

<asp:DataList ID="DataList1" runat="server" RepeatColumns="6" RepeatDirection="Horizontal">
            <ItemTemplate>
                <table border="0" cellpadding="5" bgcolor="#0010a4" align="center" cellspacing="1">
                    <tr>
                        <td id="td2" runat="server" width="80" align="center" bgcolor="#6699ff">
                            <asp:Label ID="lbltel" Width="150px" runat="server" Text='<%# Bind("telno") %>'></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td width="80" align="center" bgcolor="white">
                            <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" />
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList> 


C# code

string str;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            dbcom_src = db.GetSqlStringCommand("select telnoID,telno from M_TELNO where telnotrue=0");
            db.ExecuteNonQuery(dbcom_src);
            DataList1.DataSource = db.ExecuteDataSet(dbcom_src).Tables[0];
            DataList1.DataBind();
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < DataList1.Items.Count; i++)
        {
            str = ((Label)DataList1.Items[i].FindControl("lbltel")).Text;
        }
        Response.Write(str);
       
    }




------解决方案--------------------
refer:
http://www.cnblogs.com/insus/articles/2044053.html

or:
http://www.cnblogs.com/insus/articles/2036884.html
------解决方案--------------------
前台:
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Button" CommandArgument="<%# Bind("telno") %>" />

后台: 
protected void Button2_Click(object sender, EventArgs e)
{
Button btn=(Button)sender;
str=btn.CommandArgument.value;
}


------解决方案--------------------
或者换一个事件
HTML code

<div>
        <asp:Button ID="Button1" runat="server" Text="Button" CommandArgument="<%#Bind('telno') %>"
            CommandName="" OnCommand="Button1_Command" />
    </div>