GridView的TemplateField问题

friend43 2008-06-26 03:47:58
问题描述: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所在行的数据,其它的行数据不需要取出。急,在线等!
...全文
1259 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
紫气东来_999 2008-06-29
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 namhyuk 的回复:]
如果不是TemplateField里的LinkButton,只是一个Button,那么你不用在.aspx文件里指定CommandArgument,如:

HTML code
<asp:ButtonField Text="按钮" CommandName="MyCmd" />



这就够了。

C# code
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "MyCmd")
{

int index = Convert.ToInt32(e.CommandA…
[/Quote]


强。。
amandag 2008-06-26
  • 打赏
  • 举报
回复
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="productid" DataSourceID="SqlDataSource1" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" >
<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;
}
brooklyng60 2008-06-26
  • 打赏
  • 举报
回复
设置linkbutton的CommandName="select"每次点击按钮时会触发SelectedIndexChanging事件
然后在事件中写你的功能 e.RowIndex就是你选中行的索引,只有这一行数据
比如:txtName.text=gridview.rows[e.RowIndex].cells[1].text;
chaye12 2008-06-26
  • 打赏
  • 举报
回复
哎,女生说话为什么都爱带个"哦"呢??????
helingling_67 2008-06-26
  • 打赏
  • 举报
回复
楼上的可以实现的哦!
namhyuk 2008-06-26
  • 打赏
  • 举报
回复
如果不是TemplateField里的LinkButton,只是一个Button,那么你不用在.aspx文件里指定CommandArgument,如:

<asp:ButtonField Text="按钮" CommandName="MyCmd" />

这就够了。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
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等等吧。
xierfly 2008-06-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 namhyuk 的回复:]
给LinkButton指定CommandName,如CommandName="MyCmd"), CommandArgument传行索引。

处理gvFactory.RowCommand事件,
if(e.CommandName == "MyCmd")
{
//通过e.CommandArgument传进来的行索引就能找到指定的GridViewRow,再通过这个GridViewRow取到你想要的值。
}



[/Quote]

用这个可以获取。CommandArgument传行索引。一般可以绑定为设置有主键的那一列。
human_2 2008-06-26
  • 打赏
  • 举报
回复
<asp:Literal ID="RadioButtonMarkup" runat="server" meta:resourcekey="RadioButtonMarkupResource1"></asp:Literal>
<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 markup 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 += " />";


}

}
friend43 2008-06-26
  • 打赏
  • 举报
回复
谢谢chinahnzl,但你没明白我的意思。SelectedIndexChanging事件是行选中事件,也就是说我只要选中一行就触发该事件而不管我点击选中行的哪里。可我现在的需求是只能点击行里的LinkButton才能取出点击这个LinkButton所在行的数据,如果不点击LinkButton是不能取出数据的
烈火焚身 2008-06-26
  • 打赏
  • 举报
回复
up
huchong26 2008-06-26
  • 打赏
  • 举报
回复
用GridView的OnRowCommand="gridview_RowCommand"事件试试,这个应该可以

在TemplateField中加入CommandName和CommanArgument如以下方法:
<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中查找你要的数据行。
}
}
dengchenlu 2008-06-26
  • 打赏
  • 举报
回复
帮你顶下
可以判断gvRow.RowIndex == e.RowIndex
namhyuk 2008-06-26
  • 打赏
  • 举报
回复
给LinkButton指定CommandName,如CommandName="MyCmd"), CommandArgument传行索引。

处理gvFactory.RowCommand事件,
if(e.CommandName == "MyCmd")
{
//通过e.CommandArgument传进来的行索引就能找到指定的GridViewRow,再通过这个GridViewRow取到你想要的值。
}


chinahnzl 2008-06-26
  • 打赏
  • 举报
回复
SelectedIndexChanging 事件
chinahnzl 2008-06-26
  • 打赏
  • 举报
回复
不用遍历。。用GV中的SelectGridVeiwRow(选择行),有事件的。。

直接在里面写。。

62,256

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧