如何在单击按钮时获取另一功能的一个网格视图数据,

问题描述:

如何在单击按钮时通过一项功能获取一个gridview单元数据.

how to get one gridview cell data on one function on button click.

如果您需要在客户端使用单元数据(文本),则将需要在gridview行数据绑定事件期间向按钮添加onclick事件.此代码将onclick添加到单元格:

受保护的void gvDietOrders_RowDataBound(object sender,GridViewRowEventArgs e)
{
如果(e.Row.RowType == DataControlRowType.DataRow)
{
控件c = e.Row.Cells [0] .FindControl("lblDietName");
如果(null!= c)
{
字符串文本=((标签)c).Text;
如果(text!=")
{

e.Row.Cells [2] .Attributes ["onclick"] = String.Format("javascript:AddDietName(" {0}," {1});",tbxDiet.ClientID,c. ClientID);
}
}
}
}

然后在您的客户端javascript函数中捕获单元格innerText:

函数AddDietName(tbxId,lblId){

var text;
var lbl = document.getElementById(lblId);
if(null!= lbl)
{
文字= lbl.innerText;
}

var tbx = document.getElementById(tbxId);
if(null!= tbx){

var DietName = tbx.value;
if(dietName.length> 0){
DietName + =''|'';
}

DietName + ='''';
DietName + =文字;

tbx.value = DietName;
}

}
If you need the cell data (text) on the client side then you will need to add an onclick event to the button during the gridview row databound event. This code adds the onclick to the cell:

protected void gvDietOrders_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Control c = e.Row.Cells[0].FindControl("lblDietName");
if (null != c)
{
string text = ((Label)c).Text;
if (text != "")
{

e.Row.Cells[2].Attributes["onclick"] = String.Format("javascript:AddDietName(''{0}'',''{1}'');", tbxDiet.ClientID, c.ClientID);
}
}
}
}

then in your clientside javascript function capture the cell innerText:

function AddDietName(tbxId, lblId) {

var text;
var lbl = document.getElementById(lblId);
if (null != lbl)
{
text = lbl.innerText;
}

var tbx = document.getElementById(tbxId);
if (null != tbx) {

var dietName = tbx.value;
if (dietName.length > 0) {
dietName += '' |'';
}

dietName += '' '';
dietName += text;

tbx.value = dietName;
}

}


团队; ;)
您不会提及数据是在同一页面还是在不同页面.
在两种情况下都应使用网格视图RowDataBound事件.
对于显示模式,您可以使用javascript/ajax消息框或response.write.

受保护的void gv_RowDataBound(对象发送者,GridViewRowEventArgs e)
{
试试
{
如果(e.Row.RowType == DataControlRowType.DataRow)
{

字符串QueryStr ="QueryStr =" + gv.DataKeys [e.Row.RowIndex] .Value.ToString()+," + e.Row.RowIndex.ToString();
字符串PageName = Page.ResolveUrl(@"defult2.aspx?" + QueryStr);
((LinkBut​​ton)(e.Row.FindControl("lbView"))).Attributes.Add("onclick","PopupPic(""+ PageName +"'',810,500));

}//(((LinkBut​​ton)(e.Row.FindControl("lbView"))).Attributes.Add("onclick","maxwin();");
}
catch(ex ex例外)
{
//
}

}

谢谢
Prasanta Kumar Pradhan
Hi Team; ;)
Your don’t mention whether data is available in same page or different page.
In Both Case to should use grid view RowDataBound event.
For Display mode you may use javascript/ajax message box or response.write.

protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType == DataControlRowType.DataRow)
{

string QueryStr = "QueryStr=" + gv.DataKeys[e.Row.RowIndex].Value.ToString() + "," + e.Row.RowIndex.ToString();
string PageName = Page.ResolveUrl(@"defult2.aspx?" + QueryStr);
((LinkButton)(e.Row.FindControl("lbView"))).Attributes.Add("onclick", "PopupPic(''" + PageName + "'',810,500)");

}// ((LinkButton)(e.Row.FindControl("lbView"))).Attributes.Add("onclick", " maxwin();");
}
catch (Exception ex)
{
//
}

}

Thank’s
Prasanta Kumar Pradhan