简体   繁体   中英

add multiple innerhtml in asp.net

my x.aspx file:-

<form id ="Content3ret" runat="server">

</form>
<asp:Button  ID="Button1" runat="server" OnClick ="Button1_Click"/>

my x.aspx.cs file:-

 protected void Button1_Click(object sender, EventArgs e)
    {
        var x = Guid.NewGuid().ToString();
        Content3ret.innerhtml =  "<table id = '"+x+"'> <tr> <td><input type='radio' name='radiotest' checked='checked'/></td> </tr> </table>"; 
}

what I am trying to do is:- when I click button each and every time there should be new radio button added with other. like 3 button click I need 3 radio button.

but with this code when I click only one radio button creating. like 3 click only one radio button with new id.

can anyone help me?

When you add data on Content3ret.innerhtml and render them on the pages, this data are lost / gone / stay on page and never come back on post back - because this inner html is not post back with the second click on button.

So you have to render them on page, and at the same time saved it somewhere - preferable on viewstate because of asp.net

so the page will be like

<form id="form1" runat="server">
    <div runat="server" id="divPlaceOnme"></div>
    <asp:Button  ID="Button1" runat="server" Text="ok" OnClick ="Button1_Click"/>
</form>

and on code behind we have

const string InnerHtmlKeeper_name = "InnerHtmlKeeper_cnst";

string InnerHtmlKeeper
{
    get
    {
        var RetMe = ViewState[InnerHtmlKeeper_name] as string;

        if (string.IsNullOrEmpty(RetMe))
            return string.Empty;
        else
            return RetMe;
    }
    set
    {
        ViewState[InnerHtmlKeeper_name] = value;
    }
}

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
    var x = Guid.NewGuid().ToString();

    // we keep the new with the previous data on this ViewState
    InnerHtmlKeeper += "<table id = '" + x + "'> <tr> <td><input type='radio' name='radiotest' checked='checked'/></td> </tr> </table>";

    // we add the final render on the div on page
    divPlaceOnme.InnerHtml = InnerHtmlKeeper;
}

Last words. Your approaches have many issues - and your code not working as it is. The button must be on the form, and you have to render inside some other div.

The point here is to show you have to save some actions on viewstate - beside that for a good solution on your problem you must use some javascript to render the radio without post back.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM