首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >DropDownList没有在EditItemTemplate of RadGrid中显示选定的值

DropDownList没有在EditItemTemplate of RadGrid中显示选定的值
EN

Stack Overflow用户
提问于 2015-07-14 02:16:21
回答 1查看 2.2K关注 0票数 0

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

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

代码语言:javascript
运行
复制
<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#代码:

代码语言:javascript
运行
复制
    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;
                }

             }
         }

这是存储过程:

代码语言:javascript
运行
复制
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's“选定值。每次我尝试从代码后面设置DropdownList的选定值时,它都会在所选值中显示第一项'- Select‘。

请回答我的代码中有什么问题。我在Telerik是个新手。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-14 03:52:21

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

代码语言:javascript
运行
复制
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>

希望这能帮上忙。

一切都好..。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31396533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档