Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DropDownlList的SelectedValue_C#_Asp.net - Fatal编程技术网

C# DropDownlList的SelectedValue

C# DropDownlList的SelectedValue,c#,asp.net,C#,Asp.net,我有一个gridview,在编辑部分有dropdownlist,我想在编辑时绑定数据库中的selectedvalue。在designer部分中没有SelectedValue属性,它给出运行时错误。有什么帮助吗??有没有办法从代码隐藏中处理它 <asp:TemplateField HeaderText="Company"> <EditItemTemplate> <asp:DropDownList

我有一个gridview,在编辑部分有dropdownlist,我想在编辑时绑定数据库中的selectedvalue。在designer部分中没有SelectedValue属性,它给出运行时错误。有什么帮助吗??有没有办法从代码隐藏中处理它

<asp:TemplateField HeaderText="Company">
                <EditItemTemplate>
                    <asp:DropDownList ID="DDLCompany" runat="server" DataValueField="cname" DataTextField="cname" SelectedValue = '<%# Bind("cname") %>'  >
                    </asp:DropDownList>
                </EditItemTemplate>
                <ItemTemplate>
                <asp:Label ID="CompanyLabel" runat="server" Text='<%# Bind("cname") %>'></asp:Label>
                </ItemTemplate>   
             </asp:TemplateField>
  • 在代码隐藏中,您必须进行检查 对于e.Row.RowType== DataControlRowType.DataRow&& e、 Row.RowState== DataControlRowState.在中编辑 在找到您的 下拉列表。
  • 在aspx上,您可以设置 按以下方式选择CedValue:
      • 在代码隐藏中,您必须进行检查 对于e.Row.RowType== DataControlRowType.DataRow&& e、 Row.RowState== DataControlRowState.在中编辑 在找到您的 下拉列表。
      • 在aspx上,您可以设置 按以下方式选择CedValue:

      看起来您的代码隐藏已经设置好了…不,它没有设置。它选择DDL的第一个数据。它不绑定数据库中的实际值看起来您的代码隐藏就是为了这么做而设置的…不,它没有设置。它选择DDL的第一个数据。它不绑定数据库中的实际值
      protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
          {
              if (e.Row.RowType == DataControlRowType.DataRow)
              {
                  DropDownList DDLCompany = (DropDownList)e.Row.FindControl("DDLCompany");
                  DropDownList DDLPrinter = (DropDownList)e.Row.FindControl("DDLPrinter");
      
                  if (DDLCompany != null)
                  {
                      DDLCompany.DataSource = userobj.FetchCompanyList();
                      DDLCompany.DataBind();
                      DDLCompany.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString();                
                  }
      
                  if (DDLPrinter != null)
                  {
                      DDLPrinter.DataSource = userobj.FetchPrinterList();
                      DDLPrinter.DataBind();
                      DDLPrinter.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString();
                  }
              }
          }