在网格视图中使用按钮查看数据的最佳方法是什么? [英] What is the best method for view data using a button in a grid view?
问题描述
我设计了一个用户界面,以在网格视图中显示详细信息(简称).如果用户想查看该指定行的完整详细信息,则必须单击一个按钮.然后它将出现在同一页面中.我应该在按钮单击事件中编写什么代码?
I designed a user interface to show details in a grid view(in brief). if user want to see full details of that specified row he has to click a button. then it will appear in the same page. What should I code inside the button click event?
推荐答案
这很简单,您需要调用Gridveiw行命令事件处理程序
参见首先将按钮转换为Template Column,如下所示,并给它一个命令Name,如下所示
Its Simple , You need to call the Gridveiw Row Command Event Handler
See first convert the button as Template Column , as shown below and give it a command Name as shown below
<asp:TemplateField HeaderText="Actions" meta:resourcekey="TemplateFieldResource9"> <ItemTemplate>
<asp:ImageButton ID="imgView" ImageUrl="~/Images/View.gif" runat="server" CommandName="ViewIssueDetails" CommandArgument='<%# Eval("ISSUEID") %>' ToolTip="View Issue Details" />
</ItemTemplate> </asp:TemplateField>
现在调用以下方法
Now call the below method
protected void gvTask_OnRowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ViewIssueDetails")
{
GridViewRow row;
row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
}
}
GridviewRow Row,您可以读取Grdiveiw的每一列的值,并可以根据需要显示它.
确保您的Gridview标记了以下命令
GridviewRow Row , you can read the value of each columns of Grdiveiw and can display it as per your requirement
Make sure your Gridview has tagged to following command
<asp:GridView ID="gvMyTask" runat="server" Width="100%"OnRowCommand="gvTask_OnRowCommand" OnPageIndexChanging="gvTask_PageIndexChanging" AllowPaging="True" AutoGenerateColumns="False">
下面是一个小示例,您可以从该示例中学习:
I. GridView控件的代码:
Here is a small example you can learn from this example:
I. Code for GridView control:
<asp:GridView id="gridview" runat="server" AutoGenerateColumns="False" OnRowCommand="gridview_OnRowCommand">
<Columns>
<asp:Templatefield HeaderText="Id">
<Itemtemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("Id")%>' />
</Itemtemplate>
</asp:TemplateField>
<asp:TemplateField>
<Itemtemplate>
<asp:LinkButton ID="lnkDetail" runat="server" Text="Detail"
CommandName="Detail" CommandArgument='<%# Eval("Id")%>' />
</Itemtemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
二.现在为gridview_OnRowCommand
编写代码
II. Now write code for gridview_OnRowCommand
protected void gridview_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName == "Detail")
{
//retrieve the Id from CommandArgument
int id = Convert.ToInt32(e.CommandArgument);
//now write your code for retrieving the detail value from database.
//then store it in to DataSet
//Bind your GridView with that DataSourse.
}
}
希望对您有帮助.
祝你好运.
I hope it will help you.
Good luck.
更好地使用详细信息视图...
better use details view......
这篇关于在网格视图中使用按钮查看数据的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!