本文介绍了如何从itemTemplate中的数据网格控制标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码,我想说的是,选择LABEL(模型")文本时,我具有OnSelectedIndexChanged命令,但如何从此标签中获取值
I HAVE THE FOLLOWING CODE AND I WANT TO LETS SAY GET LABEL("model") TEXT WHEN IT IS SELECTED I HAVE OnSelectedIndexChanged Command But How do I get The Values From This Label
<asp:DataGrid ID="DataGrid3" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderWidth="3px" CellPadding="4" Height="69px"
Style="z-index: 100; left: -1px; position: absolute;
top: 4px" Width="300px" ForeColor="Black" BorderStyle="Solid" CellSpacing="2" OnSelectedIndexChanged="DataGrid3_editselected">
<FooterStyle BackColor="#CCCCCC" />
<SelectedItemStyle BackColor="#000099" ForeColor="White" Font-Bold="True" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" Mode="NumericPages" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<SelectedItemStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:ButtonColumn ButtonType="PushButton" Text="Edit this Product" CommandName="Select"></asp:ButtonColumn>
<asp:TemplateColumn>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="ID" runat="server" Text=''<%# Eval("ID") %>'' Visible="false" ></asp:Label>
</td>
<td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td>
<td>
<asp:Image ID="pic" runat="server" ImageUrl=''<%#"~/all/"+ Eval("Pic")%>'' Height="100" Width="100"></asp:Image>
</td>
<td>
<asp:Label ID="cat" runat="server" Text=''<%# Eval("category") %>'' Visible="false" ></asp:Label>
</td>
<td>
<asp:Label ID="model" runat="server" Text=''<%# Eval("carmodel") %>'' Visible="false" ></asp:Label>
</td>
<td>
<asp:Label ID="year" runat="server" Text=''<%# Eval("year") %>'' ></asp:Label>
</td>
<td>
<asp:Label ID="price" runat="server" Text=''<%# Eval("price") %>'' Visible="false" ></asp:Label>
</td>
<td>
<asp:Label ID="stock" runat="server" Text=''<%# Eval("stock") %>'' Visible="false" ></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<ItemStyle BackColor="White" />
</asp:DataGrid>
答案应该是这样的:(在c#后面的代码中)
The Answer Should be Somthing Like This:(in code behind c#)
Label mod = this.DataGrid3.SelectedItem.Cells[3].FindControl Label("model").Text;
推荐答案
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
Label model = (Label)GridView1.Rows[e.NewSelectedIndex].FindControl("model");
// Do something
}
希望对您有所帮助.
I hope this helps you well.
Label mod = (Label)DataGrid3.SelectedRow.Cells[3].FindControl("model"); //just changed the index of cells based on your requirements
if (mod != null)
{
string s = mod.Text;
}
这篇关于如何从itemTemplate中的数据网格控制标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!