简体   繁体   English

从asp.net中的gridview模板字段读取值

[英]reading values from gridview template field in asp.net

Here is a code of template field in gridview . 这是gridview中模板字段的代码。 i want to get value of this field in string using c# code. 我想使用C#代码获取字符串中的该字段的值。

<asp:TemplateField HeaderText="Status" SortExpression="Status">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:DropDownList ID="DropDownList2" runat="server">
                                    <asp:ListItem Value="P"></asp:ListItem>
                                    <asp:ListItem Value="L"></asp:ListItem>
                                    <asp:ListItem Value="A"></asp:ListItem>
                                </asp:DropDownList>
                            </ItemTemplate>
                        </asp:TemplateField>

i tried the following code but its not working . 我尝试了以下代码,但无法正常工作。 here is my code for reading value. 这是我的阅读价值代码。

 string str = gridview1.Rows[0].Cells[0].Text.ToString();

but its returning empty string. 但返回的空字符串。 how can I get selected value. 如何获得选定的值。

You can't find the dropdown selected value like this. 您找不到像这样的下拉选择值。 You need to find the control first using FindControl method:- 您需要先使用FindControl方法找到该控件:

DropDownList DropDownList2 =  (DropDownList)gridview1.Rows[0].Cells[0]
                                                     .FindControl("DropDownList2");
string str = DropDownList2.SelectedValue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM