Frage

I have a HtmlTable and I need to get the innerhtml from it, without setting the

EnableEventValidation="false" 

Therefore I would like to Register the textinput in a overridden method, here is an example

<table id="Table1" runat="server">
    <tr>
        <td>
        </td>
        <td>
            <span>Comments</span>
        </td>
    </tr>
    <tr>
        <td>
            Write som text in the field
        </td>
        <td>
            <input id="Text1" type="text" runat="server"></td>
        <td>
            </td>
    </tr>
</table>
<asp:Button ID="submit" runat="server" Text="Button" onclick="submit_Click" />

If I remove the input Text1 it's working fine but.

And in codebehind.

   public override void RenderControl(HtmlTextWriter writer)
        {
            Page.ClientScript.RegisterForEventValidation(Text1.UniqueID);
            base.RenderControl(writer);
        }

        protected void submit_Click(object sender, EventArgs e)
        {
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            Table1.RenderControl(htw);

            string g = sw.ToString(); //Here i want the innerhtml
        }

I've tried several diffrent solutions but im still getting

RegisterForEventValidation can only be called during Render();

Any idea how this can be accomplished WITHOUT setting EnableEventValidation="false"?

War es hilfreich?

Lösung

So i've ended up doing a workaround for the problem by removing the runat="server" attribute. And now im getting my innerhtml of the table complete with userinput. Here is the changes.

 <table id="Table1" runat="server">
        <tr>
            <td>
            </td>
            <td>
                <span>Comments</span>
            </td>
        </tr>
        <tr>
           <td>
            Write som text in the field
        </td>
        <td>
            <input id="Text1" name="Text1" type="text" value="<%=ServerValue %>"></td>
        <td>
                </td>
        </tr>
    </table>
    <asp:Button ID="submit" runat="server" Text="Button" onclick="submit_Click" />

The runat="server" is gone and a value is binded.

public string ServerValue = String.Empty;
        protected void submit_Click(object sender, EventArgs e)
        {
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            ServerValue = String.Format("{0}", Request.Form["Text1"]);
            Table1.RenderControl(htw);

            var g = sw.ToString();// here is the whole innerhtml of the table
        }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top