如何在Gridview中查找值 [英] how to Find The Values In Gridview
问题描述
大家好
我有一个gridview,因为我有两个coloums,一个有界字段,另一个是链接按钮..如果单击链接按钮,则相关的有界字段文本应复制到另一页
hi all
i have one gridview in that i have two coloums one bounded field another one is link button.. if i click link button the related bounded field text should be copied to another page
<asp:GridView ID="GridView1" runat="server" ShowHeader="False"
AutoGenerateColumns="false" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="Address" />
<asp:TemplateField HeaderText="Jobs">
<ItemTemplate>
<asp:LinkButton ID="Update" runat="server" CommandName="cmdid" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>">Update</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
public void Gridbind()
{
dt = new DataTable();
dt.Columns.Add(new DataColumn("Address", typeof(string)));
DataSet dl = Deliverydetails();
int count = dl.Tables[0].Rows.Count;
for(int i=0;i<count;i++)
{
DataRow dr = null;
dr = dt.NewRow();
string data = dl.Tables[0].Rows[i]["clad_name"].ToString() + ',' +.......;
dr["Address"]=data;
dt.Rows.Add(dr);
}
GridView1.DataSource=dt.DefaultView;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cmdid")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectrow = GridView1.Rows[index];
// here how to get value of selected rows Text
}
推荐答案
通过这种方式:
以下将ItemName绑定到LinkButton
的CommandArgument
.
Ty this way:
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.
// store it and access in another page.
}
}
发送方持有对触发事件处理程序的LinkButton
的引用.
您可以将对象转换为LinkButton
,然后检索其CommandArgument
并将其转换为字符串.
The sender holds a reference to the LinkButton
that triggered the event handler.
You can cast the object into a LinkButton
and then retrieve its CommandArgument
and cast that into string.
if (e.CommandName == "cmdid")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectrow = GridView1.Rows[index];
string value=selectrow .Cells[0].Text;
Response.Redirect("address.aspx?id="+value);
}
您现在可以获取querystring值并在下一页访问它.
希望对您有帮助
you can now get the querystring value and access it in the next page.
Hope this helps you
刚刚更改
受保护的void GridView1_RowCommand(对象发送者,GridViewCommandEventArgs e)
{
如果(e.CommandName =="cmdid")
{
int索引= Convert.ToInt32(e.CommandArgument);
字符串值= GridView1.Rows [index] .Cells [1] .Text.ToString();
Response.Redirect("address.aspx?id =" + value);
}
}
just Changed
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cmdid")
{
int index = Convert.ToInt32(e.CommandArgument);
string value = GridView1.Rows[index].Cells[1].Text.ToString();
Response.Redirect("address.aspx?id=" + value);
}
}
这篇关于如何在Gridview中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!