前台:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Height="72px" Width="344px">
            <Columns>
                <asp:TemplateField HeaderText="内容">
                    <ItemTemplate>
                       <asp:DropDownList ID="DropDownList1" runat="server">
                            <asp:ListItem Selected="True">aa</asp:ListItem>
                            <asp:ListItem>bb</asp:ListItem>
                            <asp:ListItem>cc</asp:ListItem>
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:ButtonField ButtonType="Button" Text="查看" CommandName="查看" />
            </Columns>
        </asp:GridView>后台是VB写的:
    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "查看" Then
Dim dList1 As DropDownList = CType(GridView1.Rows(e.CommandArgument).FindControl("DropDownList1"), DropDownList)
    Me.TextBox2.Text = dList1.SelectedValue
    End If
    End Sub我在DropDownList1选择不同的内容,比如bb、cc,点查看按钮,出现在文本框的只是默认选择项aa.怎么让我选择什么,点击查看后文本就得到什么?无法正确取得模板列的值并保存

解决方案 »

  1.   

    if (!this.IsPostBack)
            {            //绑定数据             
            }
      

  2.   

    我的代码例子:if(!IsPostBack)
    {
    //指定数据库,连接,打开
    string connstr=Application["ConnString"].ToString();
    SqlConnection conn=new SqlConnection(connstr);
    conn.Open();
    string ssql="Select * from [school]";
    SqlDataAdapter sda=new SqlDataAdapter(ssql,conn);
    DataSet sds=new DataSet();
    sda.Fill(sds,"school");
    DropDownList3.DataTextField=sds.Tables["school"].Columns["s_name"].ToString(); 
    DropDownList3.DataValueField=sds.Tables["school"].Columns["id"].ToString(); 
    DropDownList3.DataSource=sds.Tables[0].DefaultView; 
    DropDownList3.DataBind();
    conn.Close();
    conn.Dispose();
    }
      

  3.   

    呵呵,自己搞定的,两位老兄都说中了,是if (!this.IsPostBack的问题