如何获取动态文本框ID [英] How to get dynamic textbox id

查看:94
本文介绍了如何获取动态文本框ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在第一个按钮上生成文本框ID,我需要在第二个按钮上点击文本框文本。



但是当我点击第二个按钮没有得到id时,id值为null。给我这个解决方案



代码:这个方法按下按钮1



  public   void  GetColumn( string  tablename)
{
string sqlcommand = ------------------ TABLE_NAME =' + tablename + '------------------;


使用(SqlCommand cmd = new SqlCommand(sqlcommand) )
{
使用(SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = connection;
sda.SelectCommand = cmd;
使用(DataTable dt = new DataTable())
{
sda.Fill(dt);

if (dt.Rows.Count > 0
{

for int i = 0 ; i < dt.Rows.Count; i ++)
{
TableRow rw = new TableRow();
TableCell cw = new TableCell();
TableCell cw1 = new TableCell();

Label NewLabel = new Label();
NewLabel.ID = 标签 + dt.Rows [i] [ 0 ]的ToString();
NewLabel.Text + = dt.Rows [i] [ 0 ]。ToString();
cw.Text = NewLabel.Text;

TextBox tb = new TextBox();
tb.ID = dt.Rows [i] [ 0 ]。ToString();
cw1.Controls.Add(tb);
rw.Cells.Add(cw);
rw.Cells.Add(cw1);
Table1.Rows.Add(rw);

}

Panel1.Controls.Add(Table1);
}

connection.Close();

}
}
}
}


代码:此 On按钮 2

for int i = 0 ; i < dt.Rows.Count ; i ++)
{


string name = dt.Rows [i] [ 0 ]的ToString(); // 此处id存在
TextBox l1 =(TextBox)Panel1.FindControl(name) ; // l1为空

s3 + = dt.Rows [i] [ 0 ]。ToString()+ = 9; + l1.Text + 'and;
}
}
sw1.WriteLine(s1);
sw1.WriteLine(s2);
sw1.WriteLine(s3);

string s4 = .dbo。 + ;
string s5 = ;
string s7 = ;
< span class =code-keyword> string
s6 = values(;


sw1.Close();
}
}
}

}

解决方案

单击事件中,您将获得对按钮本身的引用( sender 参数)。应该足以识别它。


你好,



动态控制id会在回发后丢失。所以保留在PAGE_INIT方法中创建动态文本框所需的文本框的ID。



在您的场景中,您必须在page_init方法中调用方法GetColumn(string tablename)那么只有你可以在按钮2点击事件中获取你的文本框ID。



  private   void  Page_Init(Objectsender,System.EventArgs e)
{
GetColumn( string tablename)
}



谢谢
希望这个给出一些想法


hi,

I generate textbox id on 1st button, and i need get textbox text on 2nd button click.

but when i click 2nd button did not getting id, id value is null. give me solution for this

code: This Method is caaling by button 1

public void GetColumn(string tablename)
       {
           string sqlcommand = "------------------ TABLE_NAME = '" + tablename + " '  ------------------";


           using (SqlCommand cmd = new SqlCommand(sqlcommand))
           {
               using (SqlDataAdapter sda = new SqlDataAdapter())
               {
                   cmd.Connection = connection;
                   sda.SelectCommand = cmd;
                   using (DataTable dt = new DataTable())
                   {
                       sda.Fill(dt);

                       if (dt.Rows.Count > 0)
                       {

                           for (int i = 0; i < dt.Rows.Count; i++)
                           {
                               TableRow rw = new TableRow();
                               TableCell cw = new TableCell();
                               TableCell cw1 = new TableCell();

                               Label NewLabel = new Label();
                               NewLabel.ID = "Label" + dt.Rows[i][0].ToString();
                               NewLabel.Text += dt.Rows[i][0].ToString();
                               cw.Text = NewLabel.Text;

                               TextBox tb = new TextBox();
                               tb.ID = dt.Rows[i][0].ToString();
                               cw1.Controls.Add(tb);
                               rw.Cells.Add(cw);
                               rw.Cells.Add(cw1);
                               Table1.Rows.Add(rw);

                           }

                           Panel1.Controls.Add(Table1);
                       }

                       connection.Close();

                   }
               }
           }
       }


Code: This is On Button 2
                 
                           for (int i = 0; i < dt.Rows.Count; i++)
                           {
                              

                         string name =dt.Rows[i][0].ToString(); // here id is present 
                         TextBox l1 = (TextBox)Panel1.FindControl(name); // l1 is null

                                   s3 += dt.Rows[i][0].ToString() + " =  9;" + l1.Text + "' and ";
                               }
                           }
                           sw1.WriteLine(s1);
                           sw1.WriteLine(s2);
                           sw1.WriteLine(s3);

                           string s4 = ".dbo." +  " (";
                           string s5 = "";
                           string s7 = "";
                           string s6 = "values (";

                          
                           sw1.Close();
                       }
                   }
               }

           }

解决方案

In Click event you get a reference to the button itself (sender argument). Should be enough to identify it.


Hello ,

Dynamic control id will get lost after post back .So to retain the Id of textboxes you need to create dynamic textboxes at PAGE_INIT method.

Here in your scenario you have to call your method "GetColumn(string tablename)" at the page_init method then only you can fetch your textbox id at button 2 click event.

private void Page_Init(Objectsender, System.EventArgs e) 
{
    GetColumn(string tablename)
}



Thanks
Hope this give some idea


这篇关于如何获取动态文本框ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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