在gridview中使用OnSelect Change打印到标签
Posted
技术标签:
【中文标题】在gridview中使用OnSelect Change打印到标签【英文标题】:Printing out to a label using OnSelectChange in gridview 【发布时间】:2014-01-10 15:18:30 【问题描述】:我在 VS2012 中使用 asp.net 和 C# 对我的数据网格进行了 On Select 更改,但由于某种原因,所选行没有在我的 标签 中打印出来,我不是 C# 专家虽然我正在学习,但如果我犯了错误,请有人告诉我。
谢谢
C# 代码
public void Latest_DVD()
using (OleDbDataAdapter dataquer = new OleDbDataAdapter("SELECT Title,Category,Director,Stock,Year FROM DVD ", conn))
dataquer.Fill(dt);
DG_Latest.ShowHeader = true;
DG_Latest.DataSource = dt;
DG_Latest.DataBind();
conn.Close();
conn.Dispose();
void Latest_DVD_SelectedIndexChanged(Object sender, EventArgs e)
GridViewRow row = DG_Latest.SelectedRow;
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
asp.net
<asp:GridView ID="DG_Latest" runat="server" AutoGenerateSelectButton="True" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical">
<AlternatingRowStyle BackColor="#DCDCDC" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
【问题讨论】:
将您的代码调试为“Latest_DVD_SelectedIndexChanged”并检查事件是否触发 【参考方案1】:您需要正确连接SelectedIndexChangedEvent
试试这个:
<asp:GridView ID="DG_Latest" runat="server" AutoGenerateSelectButton="True"
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
CellPadding="3" GridLines="Vertical"
OnSelectedIndexChanged="Latest_DVD_SelectedIndexChanged">
<AlternatingRowStyle BackColor="#DCDCDC" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
代码背后:
protected void Latest_DVD_SelectedIndexChanged(Object sender, EventArgs e)
GridViewRow row = DG_Latest.SelectedRow;
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
【讨论】:
谢谢@Sudhakar Tillapudi 不幸的是我之前尝试过,但我得到了这个错误:错误1'_Default.Latest_DVD_SelectedIndexChanged(object,System.EventArgs)'由于其保护级别C:\ Users而无法访问\ben\Dropbox\Ben & Andrew\DataBase\members\Default.aspx 16 @Beep:在code behind
中将您的事件Latest_DVD_SelectedIndexChanged
访问修饰符更改为protected
,检查我的编辑。
受保护的 void... 我觉得自己像个傻瓜,一如既往地感谢 Sudhaka Tillapudi,你帮了大忙。
@Beep:别担心,亲爱的,每个人都应该从某个点开始。顺便说一句,我很高兴为您提供帮助。祝您有美好的一天。
@afzalulh:谢谢。【参考方案2】:
您需要在标记中添加OnSelectedIndexChanged="Latest_DVD_SelectedIndexChanged"
,如下所示:
<asp:GridView ID="DG_Latest" runat="server" AutoGenerateSelectButton="True"
BackColor="White" BorderColor="#999999" BorderStyle="None"
BorderWidth="1px" CellPadding="3" GridLines="Vertical"
OnSelectedIndexChanged="Latest_DVD_SelectedIndexChanged">
<AlternatingRowStyle BackColor="#DCDCDC" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
【讨论】:
谢谢@afzalulh 显示一个错误。错误 20“ASP.default_aspx”不包含“DG_Latest_SelectedIndexChanged”的定义 @Beep:你需要正确连接selectedindexchanged
事件,检查我的答案以上是关于在gridview中使用OnSelect Change打印到标签的主要内容,如果未能解决你的问题,请参考以下文章
使用 ajax 时,Select2(4.00) 在 onSelect 事件中返回 undefined