如何在占位符中放置动态控件值

时间:2013-05-25 06:27:29

标签: c# asp.net .net

我的要求就像我需要在占位符控件中放置动态创建的单选按钮值或文本以及如何垂直排列控件,下面我已附上快照供您参考,请帮我解决此问题

AspxPage:

     <div id="authenticationModes">
        <asp:PlaceHolder ID="plhldrAuthModes1" runat="server" >
        </asp:PlaceHolder>
    <asp:Button ID="btnAuthModeSave" runat="server" Text="Save Authentication Mode" 
            onclick="btnAuthModeSave_Click" />
</div>

服务器端编码:

    protected void Page_Load(object sender, EventArgs e)
            {
             //...... Database  coding 
             foreach (DataRow authModeObj in ds.Tables[0].Rows)
              {
               RadioButton rdoAuthModeSingleFactor = new RadioButton();
               rdoAuthModeSingleFactor.Text = authModeObj["AuthenticationModes"].ToString();
               string authModeIdVal = authModeObj["AuthenticationModeId"].ToString();
               rdoAuthModeSingleFactor.GroupName = "AuthModes";
               rdoAuthModeSingleFactor.ID = "AuthModeRdoID";
               rdoAuthModeSingleFactor.Attributes.Add("Value", authModeIdVal);
               plhldrAuthModes1.Controls.Add(rdoAuthModeSingleFactor);
    }
}
 protected void btnAuthModeSave_Click(object sender, EventArgs e)
        {
           //I tried this piece of code but iam getting error

            RadioButton rdo = (RadioButton)plhldrAuthModes1.NamingContainer.FindControl("AuthModeRdoID");
            string authenticationModeCheckedVal = rdo.Text.ToString();
}

enter image description here

3 个答案:

答案 0 :(得分:0)

这个错误没问题,因为它是预期的行为。

要解决此错误,请从pageload中删除这些代码并将其放入page_init

删除代码

foreach (DataRow authModeObj in ds.Tables[0].Rows)
          {
           RadioButton rdoAuthModeSingleFactor = new RadioButton();
           rdoAuthModeSingleFactor.Text = authModeObj["AuthenticationModes"].ToString();
           string authModeIdVal = authModeObj["AuthenticationModeId"].ToString();
           rdoAuthModeSingleFactor.GroupName = "AuthModes";
           rdoAuthModeSingleFactor.ID = "AuthModeRdoID";
           rdoAuthModeSingleFactor.Attributes.Add("Value", authModeIdVal);
           plhldrAuthModes1.Controls.Add(rdoAuthModeSingleFactor);
    }

您可能会问为什么我需要这样做? Ans是因为页面加载会覆盖回发值。

答案 1 :(得分:0)

使用CSS可以轻松完成。只需根据需要设置div#authenticationModes的宽度。例如

<div id="authenticationModes" style="width:120px;">

答案 2 :(得分:0)

使用在页面级别声明的RadioButtonList,这是完整的示例代码:

public partial class DynamicControl : System.Web.UI.Page
{
    DataSet ds;
    RadioButtonList rbList;

    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);

        //just some demo data..
        ds = new DataSet();
        ds.Tables.Add("test");
        ds.Tables[0].Columns.Add("AuthenticationModes");
        ds.Tables[0].Columns.Add("AuthenticationModeId");
        DataRow r1 = ds.Tables[0].NewRow();
        r1["AuthenticationModes"] = "windows";
        r1["AuthenticationModeId"] = "1";
        ds.Tables[0].Rows.Add(r1);
        DataRow r2 = ds.Tables[0].NewRow();
        r2["AuthenticationModes"] = "Forms";
        r2["AuthenticationModeId"] = "2";
        ds.Tables[0].Rows.Add(r2);

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        rbList = new RadioButtonList();
        foreach (DataRow authModeObj in ds.Tables[0].Rows)
        {
            rbList.Items.Add(new ListItem(
                authModeObj["AuthenticationModes"].ToString(),
                   authModeObj["AuthenticationModeId"].ToString()));
        }
        plhldrAuthModes1.Controls.Add(rbList);
    }

    protected void btnAuthModeSave_Click(object sender, EventArgs e)
    {
        string authenticationModeCheckedVal = rbList.SelectedItem.Text;
    }
}