我试图在第二列中单击链接按钮时获得第一列标签中的值,所以我添加了onselectedindexchanged函数并向其中添加了一些代码,但根本没有触发,我尝试了多种方法并可以正常工作,但什么也没发生
<asp:GridView ID="ProjectGridView" runat="server" ShowHeaderWhenEmpty="true" OnSelectedIndexChanged="ProjectGridView_SelectedIndexChanged" AutoGenerateColumns="False" Visible="true" CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="true">
<AlternatingRowStyle BackColor="White" Height="20px" />
<RowStyle Height="20px" />
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label Visible="false" ID="lblProjectID" runat="server" Text=' <%# Eval("Project_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Number">
<ItemTemplate>
<asp:LinkButton ID="LinkProjectNumber" runat="server" Text=' <%# Eval("Project_Number") %>'></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectNumberTextBox" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Name">
<ItemTemplate>
<%# Eval("Project_Name") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectNameTextBox" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<%# Eval("Project_Location") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectLocationTextBox" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<%# Eval("Project_Description") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectDescriptionTextBox" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" CommandName="Footer" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" HorizontalAlign="Center" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#2784FC" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
css代码
protected void ProjectGridView_SelectedIndexChanged(object sender, EventArgs e)
{
string projectid = (ProjectGridView.SelectedRow.FindControl("LinkProjectID") as Label).Text;
PopUpMessage(projectid);
}
答案 0 :(得分:1)
两件事
您无法在网格视图上设置OnSelectedIndexChanged="ProjectGridView_SelectedIndexChanged"
并使用按钮来控制它。看到asp链接按钮不会更改gridview上的选定索引(OnSelectedIndexChanged
方法最适用于下拉菜单。
只需使用按钮单击事件,因为您可以通过这种方式访问链接按钮的文本
前端代码
<asp:GridView ID="ProjectGridView" runat="server" ShowHeaderWhenEmpty="true" AutoGenerateColumns="False" Visible="true" CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="true">
<AlternatingRowStyle BackColor="White" Height="20px" />
<RowStyle Height="20px" />
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label Visible="false" ID="lblProjectID" runat="server" Text=' <%# Eval("Project_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Number">
<ItemTemplate>
<asp:LinkButton ID="LinkProjectNumber" runat="server" OnClick="LinkProjectNumber_Click" Text=' <%# Eval("Project_Number") %>'></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectNumberTextBox" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Project Name">
<ItemTemplate>
<%# Eval("Project_Name") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectNameTextBox" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<%# Eval("Project_Location") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectLocationTextBox" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<%# Eval("Project_Description") %>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ProjectDescriptionTextBox" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" CommandName="Footer" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#507CD1" HorizontalAlign="Center" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#2784FC" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
服务器端代码
protected void LinkProjectNumber_Click(object sender, EventArgs e)
{
string projectid = (sender as LinkButton).Text;
PopUpMessage(projectid);
}
那应该解决您的问题。哦,作为旁注,我还注意到您正在将“链接按钮ID”投射为标签(稍后会引起问题)。
编辑:我注意到您正在尝试获取项目ID,因此我有以下方法可以为您完成此操作(如果您遇到任何对象引用错误而不是让我知道,该方法就可以工作,我将尽力而为帮助,但我运行了这段代码,效果很好)
protected void LinkProjectNumber_Click(object sender, EventArgs e)
{
string projectid = ((sender as LinkButton).NamingContainer.FindControl("lblProjectID") as Label).Text;
PopUpMessage(projectid);
}
答案 1 :(得分:0)
您的事件应为GridViewSelectEventArgs
。喜欢
protected void ProjectGridView_SelectedIndexChanged(object sender, GridViewSelectEventArgs e)
{
string projectid = (ProjectGridView.SelectedRow.FindControl("LinkProjectID") as Label).Text;
PopUpMessage(projectid);
}
答案 2 :(得分:0)
您必须通过GridView的<div class="huge"><?php echo $COUT; ?></div>
<div class="huge"><?php foreach($USERS as $usr); echo *INSERT ECHO TABLES HERE* ?></div>
事件来做到这一点:
在ASPX中,将RowCommand
属性添加到CommandName
:
LinkButton
在您后面的<asp:LinkButton ID="LinkProjectNumber" runat="server" Text=' <%# Eval("Project_Number") %>' CommandName='GetData'></asp:LinkButton>
代码中:
cs