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

datalist中textbox取值问题,急
我在datalist中插入了一个TEXTBOX,怎么获取textbox所在的DATALIST主键值,下面代码如不带条件是可以全部更新数据库的,但不会加条件获取到对应TEXTBOX的主键值,求解答,急。。。
C# code
        
protected void Button1_Click(object sender, EventArgs e)
        {    
              
             string sqlStr = "";
             SqlConnection myConn = GetConnection();
             myConn.Open();
             for (int i = 0; i < DataList1.Items.Count - 1; i++)
             {
                
                 TextBox tb = (TextBox)(DataList1.Items[i].FindControl("TextBox1"));

                 sqlStr = "update gg_gg set gg_fknr= '" + tb.Text.Trim()+ "',gg_fkzt='1',gg_fksj='" + DateTime.Now.ToString() + "'where gg_id='[color=#FF0000]"+this.DataList1.DataKeys[i].ToString()+"[/color]'";

             }
             SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
             myCmd.ExecuteNonQuery();
             myCmd.Dispose();
             myConn.Close();
             Response.Write("<script language=javascript>alert('信息反馈成功!');javascript:location.replace('ggfk.aspx')</script>");


------解决方案--------------------
首先你datalist的每一行里都有修改或删除按钮吧:你让按钮绑定主键值(我这里以imagebutton为例)
 
C# code

<asp:ImageButton class="DelClass" ID="IBDelClass" runat="server" CommandArgument='<%#Eval("Id")%>' CommandName="Del" ImageUrl="~/Images/Delete.gif"  ToolTip="删除该类..."/>
//CommandArgument绑定每一样的主键
后台取值:
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument.ToString());
           //取到每行主键值后你修改或删除就都可以了
        }

------解决方案--------------------
C# code

  LZ你上面貌似是想做一个批量操作吧,代码貌似有点问题哦。简单的给你写一个吧,你拿改一下过去
  
  1.页面序号列绑定你的主键,用Label显示
  <asp:Label ID="Label1" runat="server" Text='<%# Eval("PK主键") %>'></asp:Label>

  protected void Button1_Click(object sender, EventArgs e)
        {    
              string sqlStr = "";
             SqlConnection myConn = GetConnection();
             myConn.Open();
             for (int i = 0; i < DataList1.Items.Count - 1; i++)
             {
                 Label Label1 = DataList1.Items[i].FindControl("Label1") as Label;
                 string PKValue=Label1.Text; //获取每行的主键值
                 TextBox tb = (TextBox)(DataList1.Items[i].FindControl("TextBox1"));
                 string gg_fknr=tb.Text.Trim();  //获取文本框的值
                    string gg_fksj=DateTime.Now.ToString();
                 [color=#FF0000]此处分开写便于自己调试跟踪值[/color]
                 sqlStr [color=#FF0000]+=[/color] "update gg_gg set 
                 gg_fknr= '"+gg_fknr+ "',gg_fkzt='1',gg_fksj='" + gg_fksj+ "'where 
                 gg_id='"+PKValue+'"[color=#FF0000];[/color]";
             }
             SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
             [color=#FF0000]备注如果是批量操作的话,建议LZ加上事务[/color]
             int result= myCmd.ExecuteNonQuery();
             if(result>0)
             {
                 Response.Write("<script language=javascript>alert('信息反馈成功!');javascript:location.replace('ggfk.aspx')</script>");
             }
             myCmd.Dispose();
             myConn.Close();