通过单击Gridview按钮显示gridview行的第一个列值 [英] To display 1st collumn value of a gridview row by clicking Gridview button
问题描述
大家好.
我有一个带有链接按钮的网格视图.
我只想要,
当我在网格视图中单击一个按钮时,该值应显示在Label中.
此图片将显示我的问题
背后的代码:
受保护的 无效 GridView1_RowCommand(对象发件人,GridViewCommandEventArgs e)
{
开关(例如CommandName)
{
大小写 " :
{
字符串 FirstColumnFieldName = e.CommandArgument.ToString();
Label1.Text = FirstColumnFieldName;
break ;
}
默认:
break ;
}
cmd = 新 SqlCommand(" ,同上);
cmd.ExecuteNonQuery();
}
并在Asp页面中:
<asp:GridView ID="GridView1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged" OnRowCommand="GridView1_RowCommand" CellPadding="3" Width="609px" GridLines="None" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellSpacing="1" HorizontalAlign="Center"> <RowStyle BackColor="#DEDFDE" ForeColor="Black" HorizontalAlign="Center" VerticalAlign="Middle" /> <Columns> <asp:TemplateField HeaderText="Payment" HeaderStyle-CssClass="title_bg" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:LinkButton ID="lkDelte" runat="server" OnClientClick="return confirm(''Are you sure that Payment is Done ?'')" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CommandName="y" ><img src="Images/A5.gif" alt="PayPal" border="0" /></asp:LinkButton> </ItemTemplate> <HeaderStyle CssClass="title_bg"></HeaderStyle> <ItemStyle HorizontalAlign="Center"></ItemStyle> </asp:TemplateField> </Columns> <FooterStyle BackColor="#C6C3C6" ForeColor="Black" /> <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" VerticalAlign="Middle" /> <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" HorizontalAlign="Center" VerticalAlign="Bottom" /> </asp:GridView>
谢谢......然后,您可以使用该值轻松填充标签.如果需要,还可以使用gridview的cellmouseclick事件.只需在Google上获取代码即可.
尝试:
随后将ItemName绑定到LinkButton
的CommandArgument
.
< asp:LinkButton ID =" runat =" 服务器" 文本 添加到购物车" OnClick =" CommandArgument <%#Eval(" ) %> ' / >
然后,您可以在后面的代码中像这样检索它:
受保护的 无效 LinkButton1_Click(对象发件人,EventArgs e) { LinkButton myButton =发件人 as LinkButton; 如果(myButton!= 空) { 字符串 title = myButton.CommandArgument; // 现在使用此标题将其设置为标签的文本. } }
发送方持有对触发事件处理程序的LinkButton
的引用.
您可以将对象转换为LinkButton
,然后检索其CommandArgument
<asp:gridview id="GridView1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged" onrowcommand="GridView1_RowCommand" xmlns:asp="#unknown"> <columns> <asp:templatefield headertext="Payment" headerstyle-cssclass="title_bg"> ItemStyle-HorizontalAlign="Center"> <itemtemplate> <asp:linkbutton id="lkDelte" runat="server" onclientclick="return confirm(''Are you sure that Payment is Done ?'')"> CommandArgument=''<%# Eval("UserName") %>'' ><img src="Images/A5.gif" alt="PayPal" border="0" /></asp:linkbutton> </itemtemplate> <HeaderStyle CssClass="title_bg"></HeaderStyle> <itemstyle horizontalalign="Center"></itemstyle> </asp:templatefield> </columns> </asp:gridview>
受保护的 无效 GridView1_RowCommand(对象发件人,GridViewCommandEventArgs e) { Label3.Text = GridView1.Rows [index] .Cells [ 1 ].Text; }
Hello everyone.
I have a grid view with link button.
i just want,
when i click a button in grid view , the value should be display in Label.
This image will show my problem
Code Behind:
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "y":
{
string FirstColumnFieldName = e.CommandArgument.ToString();
Label1.Text=FirstColumnFieldName;
break;
}
default:
break;
}
cmd = new SqlCommand("delete from Withdraw where UserName='FirstColumnFieldName'", con);
cmd.ExecuteNonQuery();
}
and in Asp Page :
<asp:GridView ID="GridView1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged" OnRowCommand="GridView1_RowCommand" CellPadding="3" Width="609px" GridLines="None" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellSpacing="1" HorizontalAlign="Center"> <RowStyle BackColor="#DEDFDE" ForeColor="Black" HorizontalAlign="Center" VerticalAlign="Middle" /> <Columns> <asp:TemplateField HeaderText="Payment" HeaderStyle-CssClass="title_bg" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:LinkButton ID="lkDelte" runat="server" OnClientClick="return confirm(''Are you sure that Payment is Done ?'')" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" CommandName="y" ><img src="Images/A5.gif" alt="PayPal" border="0" /></asp:LinkButton> </ItemTemplate> <HeaderStyle CssClass="title_bg"></HeaderStyle> <ItemStyle HorizontalAlign="Center"></ItemStyle> </asp:TemplateField> </Columns> <FooterStyle BackColor="#C6C3C6" ForeColor="Black" /> <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" VerticalAlign="Middle" /> <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" HorizontalAlign="Center" VerticalAlign="Bottom" /> </asp:GridView>
Thanks....
When you use the gridview''s rowheader clicked event then you can access the clicked row and all its cells. Then you can use that value to populate a label easily. If you need then you can use gridview''s cellmouseclick event also. Just google it for code.
Try:
Following binds the ItemName to theCommandArgument
of theLinkButton
.
<asp:LinkButton ID="LinkButton1" runat="server" Text="Add to cart" OnClick="LinkButton1_Click" CommandArgument='<%# Eval("ItemName") %>' />
You can then retrieve it like this in the code behind:
protected void LinkButton1_Click(object sender, EventArgs e) { LinkButton myButton = sender as LinkButton; if (myButton != null) { string title = myButton.CommandArgument; // use this title now to set as a text for your label. } }
The sender holds a reference to theLinkButton
that triggered the event handler.
You can cast the object into aLinkButton
and then retrieve itsCommandArgument
<asp:gridview id="GridView1" runat="server" onselectedindexchanged="GridView1_SelectedIndexChanged" onrowcommand="GridView1_RowCommand" xmlns:asp="#unknown"> <columns> <asp:templatefield headertext="Payment" headerstyle-cssclass="title_bg"> ItemStyle-HorizontalAlign="Center"> <itemtemplate> <asp:linkbutton id="lkDelte" runat="server" onclientclick="return confirm(''Are you sure that Payment is Done ?'')"> CommandArgument=''<%# Eval("UserName") %>'' ><img src="Images/A5.gif" alt="PayPal" border="0" /></asp:linkbutton> </itemtemplate> <HeaderStyle CssClass="title_bg"></HeaderStyle> <itemstyle horizontalalign="Center"></itemstyle> </asp:templatefield> </columns> </asp:gridview>
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) { Label3.Text = GridView1.Rows[index].Cells[1].Text; }
这篇关于通过单击Gridview按钮显示gridview行的第一个列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!