0

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 ?

3
  • 1
    you must save somewhere (viewstate) the previous state Commented Jun 11, 2021 at 9:39
  • @Aristos can you plz explain me bit more Commented Jun 11, 2021 at 9:42
  • 1
    ok, I am going to answer it - give me few minutes Commented Jun 11, 2021 at 9:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thank bro . by using this InnerHtmlKeeper += "<table id = '" + x + "'> <tr> <td><ucM:M ID='" + y + "' runat='server' /></td> </tr> </table>"; is it possible to use something like this?
@ABCD - no its not - this is just a string, not compile (the runat="server")
is there any way to do this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.