本文介绍了如何找到所选指标改变属性的GridView的datakey价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的GridView是这样的,但我得到的错误,当我选择视图按钮选择指数变更为找到主键值列。请帮我解决这个问题。
< ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =FALSE的cellpadding =4前景色=#333333网格=无OnSelectedIndexChanged = GridView1_SelectedIndexChanged>
<柱体和GT;
< ASP:的TemplateField>
<&ItemTemplate中GT;
< ASP:按钮的ID =btnViewComments文本=查看评论=服务器的CommandName =选择/>
< / ItemTemplate中>
< / ASP:的TemplateField>
< ASP:BoundField的数据字段=forumId可见=假/>
<% - < ASP:CommandField中按钮类型=按钮ShowSelectButton =真SelectText =查看评论/> - %GT;
< ASP:的TemplateField的HeaderText =问题>
<&ItemTemplate中GT;
< ASP:文本框ID =txtQuestion文本='<%#的eval(问题)%>' =服务器的TextMode =多行HEIGHT =100WIDTH =350>< / ASP:文本框>
<% - <%#的eval(问题)%> - %GT;
< / ItemTemplate中>
<% - <&EditItemTemplate的GT;
< ASP:文本框ID =txtQuestion文本='<%#的eval(问题)%>' =服务器的TextMode =多行>< / ASP:文本框>
< / EditItemTemplate中> - %GT;
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =海报名称>
<&ItemTemplate中GT;
<%#的eval(posterName)%>
< / ItemTemplate中>
<&EditItemTemplate的GT;
< ASP:标签ID =lblPosterName文本='<%#的eval(posterName)%>' =服务器>< / ASP:标签>
< / EditItemTemplate中>
< / ASP:的TemplateField>
< ASP:的TemplateField的HeaderText =日期>
<&ItemTemplate中GT;
<%#的eval(dateTim)%>
< / ItemTemplate中>
<&EditItemTemplate的GT;
< ASP:标签ID =lblDateTime文本='<%#的eval(dateTim)%>' =服务器>< / ASP:标签>
< / EditItemTemplate中>
< / ASP:的TemplateField>
< /专栏>
< RowStyle的BackColor =#F7F6F3前景色=#333333/>
< FooterStyle背景色=#5D7B9DFONT-粗体=真前景色=白/>
< PagerStyle背景色=#284775前景色=白HorizontalAlign =中心/>
< SelectedRowStyle背景色=#E2DED6FONT-粗体=真前景色=#333333/>
< HeaderStyle背景色=#5D7B9DFONT-粗体=真前景色=白/>
< EditRowStyle背景色=#999999/>
< AlternatingRowStyle背景色=白前景色=#284775/>
< / ASP:GridView的>
我的code是.....
保护无效GridView1_SelectedIndexChanged(对象发件人,EventArgs的发送)
{
尝试
{
Int64的forumId =(Int64的)GridView1.SelectedValue;
会话[forumId] = forumId;
的Response.Redirect(Thread.aspx);
}
赶上(例外)
{ 扔;
}
}
解决方案
首先,你必须在网格视图声明来定义字段名你想datakey的哪个字段。例如,如果你想forumIddatakey.than
< ASP:GridView控件ID =GridView1=服务器的AutoGenerateColumns =FALSE的cellpadding =4前景色=#333333网格=无OnSelectedIndexChanged = GridView1_SelectedIndexChanged
的DataKeyNames =forumId>
和比你可以以这种方式访问
INT intforumid = Convert.ToInt32(GridView1.DataKeys [row.RowIndex] .Values [0]);
My gridview is like this but I am getting error when I select view button to find primary key value column on selected index changed. Please help me to solve the issue.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns >
<asp:TemplateField >
<ItemTemplate >
<asp:Button ID="btnViewComments" Text ="View Comments" runat ="server" CommandName ="select" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField ="forumId" Visible ="false" />
<%--<asp:CommandField ButtonType ="Button" ShowSelectButton ="true" SelectText ="View Comments"/>--%>
<asp:TemplateField HeaderText ="Question">
<ItemTemplate >
<asp:TextBox ID ="txtQuestion" Text ='<%#Eval("question")%>' runat ="server" TextMode ="MultiLine" Height="100" Width ="350"></asp:TextBox>
<%-- <%#Eval("question")%>--%>
</ItemTemplate>
<%--<EditItemTemplate >
<asp:TextBox ID ="txtQuestion" Text ='<%#Eval("question")%>' runat ="server" TextMode ="MultiLine" ></asp:TextBox>
</EditItemTemplate>--%>
</asp:TemplateField>
<asp:TemplateField HeaderText="Poster Name">
<ItemTemplate >
<%#Eval("posterName") %>
</ItemTemplate>
<EditItemTemplate >
<asp:Label ID ="lblPosterName" Text ='<%#Eval("posterName") %>' runat ="server" ></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<ItemTemplate >
<%#Eval("dateTim") %>
</ItemTemplate>
<EditItemTemplate >
<asp:Label ID ="lblDateTime" Text ='<%#Eval("dateTim") %>' runat ="server" ></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
my code is.....
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
Int64 forumId = (Int64)GridView1.SelectedValue;
Session["forumId"] = forumId;
Response.Redirect("Thread.aspx");
}
catch (Exception)
{
throw;
}
}
解决方案
First you have to define field name in grid view declaration that which field you want to make datakey. for example if you want "forumId" datakey.than
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
DataKeyNames="forumId">
and than you can access in this way
int intforumid = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);
这篇关于如何找到所选指标改变属性的GridView的datakey价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!