将值从ASP.NET Gridview TemplateField导入C# [英] Getting values from ASP.NET Gridview TemplateField into C#

查看:68
本文介绍了将值从ASP.NET Gridview TemplateField导入C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


请建议获取单选按钮组或任何其他asp.net元素(例如文本框)的值的过程,该元素被写入Gridview模板字段内的C#代码隐藏代码中.我尝试直接使用单选按钮的ID,也使用了FindControl函数.但是他们都没有工作.

请提出一些解决方案.请提供示例代码.

在此先谢谢您. //同样,您也不能直接访问放置在gridview模板字段内的任何控件

foreach(GridView1.Rows中的GridViewRow行){
if(row.RowType == DataControlRowType.DataRow){
HyperLink myHyperLink = row.FindControl("myHyperLinkID")作为HyperLink;
}
}


我找到了满足我要求的解决方案.

protected void present_CheckedChanged(object sender, EventArgs e)
    {
        
            RadioButton button = sender as RadioButton;
            GridViewRow row = button.NamingContainer as GridViewRow;
            RadioButton pres_button = row.FindControl("present") as RadioButton;
    }


使用此代码,我们可以访问gridview的template字段中的元素.首先,我们必须将sender对象作为所需的任何元素.然后创建一个Gridviewrow对象以访问所需的行.之后,您可以使用语法
访问Gridview中的任何元素.

ElementType element_name=row.FindControl("elementID_given_in_aspx_page") as ElementType;


例如,

RadioButton pres_button = row.FindControl("present") as RadioButton;


这个解决方案对我来说很好用.我希望它对所有人有用.
谢谢


Hi,
Please suggest the procedure for getting the value of a Radio button group or any other asp.net element such as a textbox which is written inside a Gridview templatefield into the C# codebehind code. I tried using the ID of radiobutton directly, also used the FindControl function. But none of them are working.

Please suggest some solution. Please include sample codes.

Thanks in advance.

解决方案

//code below for hyperlink acces replace hyperlink with ur control
// also u cant access any control directly which are placed inside gridview template field

foreach(GridViewRow row in GridView1.Rows) {
if(row.RowType == DataControlRowType.DataRow) {
HyperLink myHyperLink = row.FindControl("myHyperLinkID") as HyperLink;
}
}


I found the solution for my requirement.

protected void present_CheckedChanged(object sender, EventArgs e)
    {
        
            RadioButton button = sender as RadioButton;
            GridViewRow row = button.NamingContainer as GridViewRow;
            RadioButton pres_button = row.FindControl("present") as RadioButton;
    }


Using this code, we can access the elements in the template field of a gridview. Firstly we have to get the sender object as any element required. Then create an Gridviewrow object to access the row required. After that you can access any element in the Gridview using the syntax

ElementType element_name=row.FindControl("elementID_given_in_aspx_page") as ElementType;


as an example,

RadioButton pres_button = row.FindControl("present") as RadioButton;


This solution is working fine for me. I hope it''ll be useful to all.
Thanks


这篇关于将值从ASP.NET Gridview TemplateField导入C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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