1

我的要求是我需要获取动态创建的单选按钮值或放置在占位符控件中的文本以及如何垂直排列控件,下面我附上了快照供您参考,请帮助我

页面:

     <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();
}

在此处输入图像描述

4

3 回答 3

0

使用 CSS 你可以很容易地做到这一点。只需根据需要设置 div#authenticationModes 的宽度。例如

<div id="authenticationModes" style="width:120px;">
于 2013-05-25T07:06:17.970 回答
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 是因为页面加载覆盖了回发值。

于 2013-05-25T06:57:53.867 回答
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;
    }
}
于 2013-05-25T08:02:48.143 回答