注册 登录
编程论坛 ASP.NET技术论坛

如何取得Datalist模版里TextBox值

疏影桐 发布于 2010-11-19 15:53, 812 次点击
如何取得Datalist模版里TextBox值
textbox的值不是数据库里面的,而是在页面添加的
然后想点击确定然后取得textbox 的值。。。。
<asp:DataList ID="DL" runat="server"  
            DataKeyField="WineID" >
        <EditItemStyle HorizontalAlign="Center" />
    <ItemStyle BackColor="White" />
    <ItemTemplate>
        <table class="style1">
        
            <tr align="left">
                <td class="style2" rowspan="7"><img alt="图片" src='./WineImages/<%#DataBinder.Eval( Container.DataItem,"Pic") %>>'/>
                    </td>
                <td class="style4">
                    编号:</td>
                <td>
                    <%#DataBinder.Eval( Container.DataItem,"WineID") %> </td>
            </tr>
            <tr>
                <td class="style4">
                    名称:</td>
                <td>
                  <%#DataBinder.Eval( Container.DataItem,"WineName") %></td>
            </tr>
                        <tr>
                <td class="style4">
                    数量:</td>
                <td>
                    <asp:TextBox ID="TextBox_BuyNum" runat="server" MaxLength="10"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style3" colspan="2">
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            
                </td>
            </tr>
           </table>
    </ItemTemplate>
    </asp:DataList>
   
   
    </div>
    <div>
        <asp:Button ID="Bt_Buy" runat="server" Text="确定" onclick="Btok_Click" /></div>
3 回复
#2
疏影桐2010-11-19 17:00
问题补充:
用了这样一个方法:TextBox tb = DL_Wine.Items[0].FindControl("TextBox_BuyNum") as TextBox;
                  string BuyNum = tb.Text.Trim();
但调试的时发现Textbox的值没有传入。。。。
#3
疏影桐2010-11-21 13:27
终于搞定了。。。。。。。
首先要在<%@ Page Language="C#"·····%>里加上enableEventValidation= "false ",如果不加这个,页面会在点击了“确定”后进行数据验证,而无法进入Button的响应函数。除此之个,在Button的响应函数还要加上如下代码 :
protected void Btok_BuyNum(object sender, EventArgs e)
       {
           Button bt = sender as Button;
           int myIndex = ((DataListItem)bt.NamingContainer).ItemIndex;
           string tb = (bt.FindControl("TextBox_BuyNum") as TextBox).Text.ToString().Trim();
           int BuyNum = Convert.ToInt32(tb);
这样就可以取到Textbox里的值了。。。。。
#4
Issac_abc2010-11-24 14:59
stringTitle = ((TextBox)e.Item.FindControl("txtTitle")).Text.Trim()
1