问题描述:GridView里的TemplateField有一个LinkButton。点击这个LinkButton后需要在一个DIV里显示出这个LinkButton所在行的相关数据。下面是我写得代码:
foreach(GridViewRow gvRow in gvFactory.Rows)
{
if(gvRow.RowType == DataControlRowType.DataRow)
{
this.txtFactoryCode.Text = gvRow.Cells[1].Text;
this.txtFactoryName.Text = gvRow.Cells[2].Text;
this.txtFactoryCode.ReadOnly = true;
}
}
但这样有个问题,最终显示得是GridView里最后一行的数据,因为是foreach遍历GridView的每一行数据。这与我想要得结果不符。请教:我怎么会只取出点击得这个LinkButton所在行的数据,其它的行数据不需要取出。急,在线等!
foreach(GridViewRow gvRow in gvFactory.Rows)
{
if(gvRow.RowType == DataControlRowType.DataRow)
{
this.txtFactoryCode.Text = gvRow.Cells[1].Text;
this.txtFactoryName.Text = gvRow.Cells[2].Text;
this.txtFactoryCode.ReadOnly = true;
}
}
但这样有个问题,最终显示得是GridView里最后一行的数据,因为是foreach遍历GridView的每一行数据。这与我想要得结果不符。请教:我怎么会只取出点击得这个LinkButton所在行的数据,其它的行数据不需要取出。急,在线等!
if(e.CommandName == "MyCmd")
{
//通过e.CommandArgument传进来的行索引就能找到指定的GridViewRow,再通过这个GridViewRow取到你想要的值。
}
可以判断gvRow.RowIndex == e.RowIndex
<asp:LinkButton CommandName="SelectReport" CommandArgument='<%# Eval("ID") %>'></asp:LinkButton>在.cs文件中,代码类似如下:
protected void gridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "SelectReport")
{
string id = e.CommandArgument.ToString(); //这个就是你要取的当前行的ID ....//可能通过这个ID从GridView中查找你要的数据行。
}
}
<asp:Button ID="btnHiddenPostButton" CommandName="HiddenPostButtonCommand" runat="server" Text="HiddenPostButton" style="display:none" Width="0px" />
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Grab a reference to the Literal control
Literal output = (Literal)e.Row.FindControl("RadioButtonMarkup"); // Output the up except for the "checked" attribute
output.Text = string.Format(
@"<input type=""radio"" name=""SuppliersGroup"" " +
@"id=""ControlProductRowSelector{0}"" value=""{0}""", e.Row.RowIndex );
// See if we need to add the "checked" attribute
if (SuppliersSelectedIndex == e.Row.RowIndex)
{
output.Text += @" checked=""checked""";
WUCProductPrice.Text = GridView1.DataKeys[SuppliersSelectedIndex]["price"].ToString(); }
output.Text += String.Format(" onclick=\"javascript:document.getElementById('{0}').value='{1}'\" ",
WUCProductPrice.ClientID, GridView1.DataKeys[e.Row.RowIndex].Value.ToString());
// Add the closing tag
Button btnHiddenPostButton = e.Row.FindControl("btnHiddenPostButton") as Button;
if (btnHiddenPostButton != null)
{
// = String.Format(" javascript:document.getElementById('{0}').click()", btnHiddenPostButton.ClientID); //output.Text += " onclick=\"javascript:setTimeout('__doPostBack(\\'WUCProduct1$GridView1$ctl03$RadioButton1\\',\\'\\')', 0)\" ";
} output.Text += " />";
} }
用这个可以获取。CommandArgument传行索引。一般可以绑定为设置有主键的那一列。
{
if (e.CommandName == "MyCmd")
{
int index = Convert.ToInt32(e.CommandArgument); //注意,这里e.CommandArgument会自动被赋值适当的索引值。
GridViewRow gvr = GridView1.Rows[index];
Response.Write(gvr.Cells[1].Text);
}
}
如果是TemplateField里的LinkButton,那么你就得自己传了,按需要你可以传主关键字,或者Container.DisplayIndex等等吧。
然后在事件中写你的功能 e.RowIndex就是你选中行的索引,只有这一行数据
比如:txtName.text=gridview.rows[e.RowIndex].cells[1].text;
<Columns>
<asp:BoundField DataField="productid" HeaderText="productid" InsertVisible="False"
ReadOnly="True" SortExpression="productid" />
<asp:BoundField DataField="productname" HeaderText="productname" />
<asp:HyperLinkField DataNavigateUrlFields="productid" DataNavigateUrlFormatString="Details.aspx?id={0}"
DataTextField="productname" />
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
this.txtFactoryCode.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
this.txtFactoryName.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
this.txtFactoryCode.ReadOnly = true;
}