【问题标题】:DropDownList is not showing the Selected Value in EditItemTemplate of RadGridDropDownList 未显示 RadGrid 的 EditItemTemplate 中的选定值
【发布时间】:2015-07-14 02:16:21
【问题描述】:

我在 RadGrid 中使用 asp DropDownList,并尝试在数据库表中添加和更新 DropDownList 项。

下面是 RadGrid 的 EditItemTemplate 中 DropDownList 的 HTML 代码:

<telerik:GridTemplateColumn UniqueName="AccountCode" HeaderText="Account Code">
       <ItemTemplate>
           <asp:Label ID="lblAcCode" runat="server" Text='<%# Eval("AccountCode")%>'></asp:Label>
       </ItemTemplate>
       <EditItemTemplate>
           <asp:Label ID="lblAcCode2" runat="server" Text='<%# Eval("AccountCode") + " - " + Eval("AccountDescription")%>'></asp:Label>
           <asp:DropDownList ID="ddlAcCode" DataTextField="AccountDescription" DataValueField="AccountCodeID" runat="server"/> 
       </EditItemTemplate>
</telerik:GridTemplateColumn>

C# 代码:

    public DataTable GetAccCode(string CompanyCode)
        {
            SqlConnection con = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand("[Invoice].[usp_tbl_AccountCode_DL_Test]", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@CompanyCode", CompanyCode);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            try
            {
                con.Open();
                da.Fill(dt);
                con.Close();
            }
            catch (Exception ex)
            {
            }
            return dt;
        }

    protected void RGGSTAcCode_ItemDataBound(object sender, GridItemEventArgs e)
        {

           if (e.Item is GridEditableItem && e.Item.IsInEditMode)
                {
                    //bind dropdwon while "Add" 
                    string CompanyCode = ddlCompany.SelectedValue.ToString();
                    GridEditableItem item = (GridEditableItem)e.Item;
                    DropDownList ddl = (DropDownList)item.FindControl("ddlAcCode");
                    ddl.DataSource = GetAccCode(CompanyCode);
                    ddl.DataTextField = "AccountDescription";
                    ddl.DataValueField = "AccountCodeID";
                    ddl.DataBind();
                    ddl.Items.Insert(0, "- Select -");

                    //Select particular dropdown value while "Edit"
                    string accCodeID =  item.GetDataKeyValue("AccountCodeID").ToString();
                    Label lblAcCode2 = item.FindControl("lblAcCode") as Label;

                    //if (!string.IsNullOrEmpty(lblAcCode2.Text))
                    //{
                        ddl.SelectedValue = lblAcCode2.Text;
                    //}
                    //string SelectedVal =  ddl.SelectedValue;

                if (!string.IsNullOrEmpty(lblAcCode2.Text))
                {
                    SqlConnection con = new SqlConnection(strcon);
                    con.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.SelectCommand = new SqlCommand("SELECT [AccountCode]+' - '+[AccountDescription] as [AccountDescription] FROM [Sunway_AP].[Invoice].[tbl_AccountCodeTest] where AccountCodeID='" + accCodeID + "' ", con);

                    DataTable dt = new DataTable();
                    try
                    {
                        adapter.Fill(dt);
                    }
                    finally
                    {
                        con.Close();
                    }
                    ddl.SelectedValue = dt.ToString();
                    string SelectedVal = ddl.SelectedValue;
                }

             }
         }

这是存储过程:

ALTER PROCEDURE [Invoice].[usp_tbl_AccountCode_DL_Test]
    @CompanyCode nvarchar(50)
AS
BEGIN   
    SET NOCOUNT ON;   
       SELECT [AccountCodeID] ,[AccountCode]+' - '+[AccountDescription] as [AccountDescription]     
       FROM [Sunway_AP].[General].[tbl_AccountCode] (NOLOCK)
       Where [CompanyCode] = @CompanyCode
       order by [AccountCode]+' - '+[AccountDescription]

END

我的问题是:在编辑时,我无法获取特定 ID(RadGrid 的特定行)的“DropDownList”选定值。 每次我尝试从后面的代码中设置 DropdownList 的选定值时,它都会在选定值内显示第一项“- Select -”。

请回复我的代码有什么问题。 我是 Telerik 的新手。提前致谢。

【问题讨论】:

    标签: c# asp.net drop-down-menu radgrid


    【解决方案1】:

    修改了我发布的代码如下:

    protected void RGGSTAcCode_ItemDataBound(object sender, GridItemEventArgs e)
        {
    
           if (e.Item is GridEditableItem && e.Item.IsInEditMode)
                {
                    //bind dropdwon while "Add" 
                    string CompanyCode = ddlCompany.SelectedValue.ToString();
                    GridEditableItem item = (GridEditableItem)e.Item;
                    DropDownList ddl = (DropDownList)item.FindControl("ddlAcCode");
                    ddl.DataSource = GetAccCode(CompanyCode);
                    ddl.DataTextField="*your text field*";
                    ddl.DataValueField="*your Value field*";
                    ddl.DataBind();
                    ddl.Items.Insert(0, "- Select -");
    
                   Label lblAcCode2 = item.FindControl("lblAcCode2") as Label;
                   if (!string.IsNullOrEmpty(lblAcCode2.Text))
                   {
                      ddl.SelectedItem.Text = lblAcCode2.Text;
                      ddl.SelectedValue = lblAcCode2.Text;
                   }
               }
         }
    
        <telerik:GridTemplateColumn UniqueName="AccountCode" HeaderText="Account Code">
           <ItemTemplate>
             <asp:Label ID="lblAcCode" runat="server" Text='<%# Eval("AccountCode")%>'></asp:Label>
           </ItemTemplate>
           <EditItemTemplate>
             <asp:Label ID="lblAcCode2" runat="server" Text='<%# Eval("AccountCode") + " - " + Eval("AccountDescription")%>' Visible="false"></asp:Label>
             <asp:DropDownList ID="ddlAcCode" DataTextField="AccountDescription" DataValueField="AccountCodeID" runat="server"/> 
           </EditItemTemplate>
       </telerik:GridTemplateColumn>
    

    希望这会有所帮助...

    一切顺利...

    【讨论】:

    • 感谢您的回复。我进行了您建议的更改(请检查我更新的发布代码)。但我仍然面临同样的问题。当我尝试调试我的代码时,每次我在ddl.SelectedValue 中看到,“- Select -”即将到来,而不是特定的 Id/行的 DropDownList 项(保存在 DB 中)。请建议我该怎么做?
    • 另外,我在标签“lblAcCode2”中得到了预期的输出,但在 DropDownList 中没有(对于特定的行/ID)。在 DropDownList 中,它总是在 ddl.SelectedValue 中显示“- Select -”
    • 我想要一些关于代码的细节。你可以解释吗?什么方法 GetAccCode(CompanyCode);回归?
    • GetAccCode(CompanyCode) 方法用于使用存储过程绑定基于“CompanyCode”的 DropDownList。请检查发布的问题,我已将此方法更新到其中。
    • 听起来一切都很好。在下拉列表中尝试 autopostback="true" 这可能会有所帮助....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多