我想选择整行并将所有值放在相应的字段中,但它不起作用。行正在选择,但值未显示在相应字段中 [英] I want to select the entire row and put all values in corresponding fields, but its not working. Row is selecting but value is not showing in the corresponding field
问题描述
protected void GridView1_RowCommand(对象发件人,GridViewCommandEventArgs e)
{
if (e.CommandName == 选择)
{
Int16 num = Convert.ToInt16(e.CommandArgument);
txt_Name.Text = GridView1.Rows [num] .Cells [ 2 ]。文字;
txt_Email.Text = GridView1.Rows [num] .Cells [ 3 ]。文字;
txt_Phone.Text = GridView1.Rows [num] .Cells [ 4 ]。文字;
ddl_Gender.Text = GridView1.Rows [num] .Cells [ 5 ]。文字;
txt_DOB.Text = GridView1.Rows [num] .Cells [ 6 ]。文字;
}
}
您好Rohit,
您可以在每一行上添加一个按钮,然后点击按钮就可以获得所选的值。
< asp:TemplateField >
< span class =code-keyword>< ItemTemplate >
< asp:LinkButton ID = lnkDelete 文本 = 删除 CssClass = itemDelete CommandArgument =' <% #Eval( Value)%>'
< span class =code-attribute> runat = server OnClick = DeleteFile / >
< / ItemTemplate >
< / asp:TemplateField >
然后创建按钮点击事件并获取值
您好Rohit,
根据您的评论,您说您正在使用TemplateFields然后如何使用单元格调用控件,这是不正确的。
上面代码中的问题是调用控件。
使用下面的示例,
protected void GridView1_RowCommand( object sender,GridViewCommandEventArgs e)
{
if (e.CommandName == 选择)
{
GridViewRow row =(GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
txt_Name.Text =(row.FindControl( 名称)< span class =code-keyword> as Label).Text;
txt_Email.Text =(row.FindControl( Email) as Label).Text;
txt_Phone.Text =(row.FindControl( Phone) as Label).Text;
ddl_Gender.Text =(row.FindControl( Gender) as Label).Text;
txt_DOB.Text =(row.FindControl( DOB) as Label).Text;
}
}
按照Vikas的建议设计你的gridview专栏,然后再执行你应该阅读的任务一些与您的要求相关的文章在开始工作之后。
您的设计和代码都没有错误,每当您使用正确的命名约定时正在使用标签给labelname作为带有前缀的lbl,我根据你的设计给出了解决方案,但这不是给出控制名称的好方法。
希望你理解..
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if(e.CommandName=="Select") { Int16 num = Convert.ToInt16(e.CommandArgument); txt_Name.Text = GridView1.Rows[num].Cells[2].Text; txt_Email.Text = GridView1.Rows[num].Cells[3].Text; txt_Phone.Text = GridView1.Rows[num].Cells[4].Text; ddl_Gender.Text = GridView1.Rows[num].Cells[5].Text; txt_DOB.Text = GridView1.Rows[num].Cells[6].Text; } }解决方案Hi Rohit,
You can add an button on each row and on click button you can get the selected values.
<asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="lnkDelete" Text="Delete" CssClass="itemDelete" CommandArgument='<%# Eval("Value") %>' runat="server" OnClick="DeleteFile" /> </ItemTemplate> </asp:TemplateField>
and then create button click event and get the values
Hi Rohit,
As per your comment, you said you are using TemplateFields then how you are calling control using cells, which is not correct.
The Problem in above code is calling control.
Use below sample,
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if(e.CommandName=="Select") { GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); txt_Name.Text = (row.FindControl("Name") as Label).Text; txt_Email.Text = (row.FindControl("Email") as Label).Text; txt_Phone.Text = (row.FindControl("Phone") as Label).Text; ddl_Gender.Text = (row.FindControl("Gender") as Label).Text; txt_DOB.Text = (row.FindControl("DOB") as Label).Text; } }
and design your gridview column as suggested by Vikas, before implement the task you should read some articles which related to your requirement after that start work on that.
There are no of mistakes in your design as well as your code, use proper naming conventions whenever you are using label give labelname as "lbl" with prefix, I gave solution as per your design, but this is not a good way to give control names.
Hope you understood..
这篇关于我想选择整行并将所有值放在相应的字段中,但它不起作用。行正在选择,但值未显示在相应字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!