简体   繁体   中英

How to innerhtml out asp tags

I am trying to get a button out to my asp page:

Div1.InnerHtml += "<td><input type=\"submit\" ID=\"Button3\" runat=\"server\" OnClick=\"Button3_Click\" value = \"start\" ></button></td> ";` 

The error "Button3_Click " but the error "0x800a1391 - Microsoft JScript runtime error: 'Button3_Click' is undefined" keeps coming out. Button3_Click is also used in the same environment and works there.

Edited More information on the workings of my code

Step 1 ) Receive a string, splits it and forms a 2D array. All of which is done in c#

Step 2 ) Output is then sent to the ASP page to be displayed in a table. The table would would have 5 columns with n number of rows. The first 3 cols are data from the string above and the next 2 rows are buttons that manipulate the data.

Listed below are some code snippets

public void createarray(string results)
    {
        using (StringReader reader = new StringReader(results))
        {
            int lineNo = -1;
            string line;
            int columncount = 3;
            Div1.InnerHtml += "<table border = \"1\" style = \" font-    size:13px ; width:20% \" ;>  ";
            while ((line = reader.ReadLine()) != null)
            {
                ++lineNo;
                twodarray(lineNo, columncount, line);

            }// while
        }// using 
        Div1.InnerHtml += "</table>";
    }// end of function 


    public Array[] twodarray(int rowcount, int columncount, string parts)
    {
        string[,] twoD = new string[10000, 10];
        string[] parts2 = parts.Split(new string[] { "," }, StringSplitOptions.None);
        int y = rowcount;
        for (int x = 0; x < columncount; x++)
        {
            twoD[y, x] = parts2[x];

        }
        Div1.InnerHtml += "<tr>";
        Div1.InnerHtml += "<td>" + y + "</td>";
        Div1.InnerHtml += "<td>" + twoD[y, 0].TrimStart('\\') + "</td>";
        Div1.InnerHtml += "<td>" + twoD[y, 1] + "</td>";
        Div1.InnerHtml += "<td>" + twoD[y, 2] + "</td>";

        Div1.InnerHtml += "<td><input type = \"submit\" ID=\"Button4\" runat=\"server\" OnClick=\"retunthis\" Text=\"stop\">stop</submit></td> ";
        Div1.InnerHtml += "</tr>";

        return null;
    }

The problem I am facing is that when the button is pressed there is no error message, there is no functions that are being processed.

At the moment I am just sending the data to the asp page through "innerhtml". is there a better way for be to display my output ?

Note: thanks to jack for editing my first question, and those who replies. <3

First you have an html tag mismatch. You are closing an input tag of type button with a button tag. This won't fix your error but promotes good syntax.

Karl's solution didn't work for me (I would have left a comment but I'm not a high enough reputation yet).

I tried a number of options, none of which do the trick. It appears that when ASP.NET renders HTML, it converts an asp:Button to an input type as you would expect but pulls out any submit events into scripts.

For example:

<asp:Button ID="Button3" Text="Start" runat="server" OnClick="Button3_Click" />

Renders to...

<input type="submit" name="ctl00$MainContent$Button3" value="Start" id="MainContent_Button3" />

Also rendered is...

<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['ctl01'];
if (!theForm) {
    theForm = document.ctl01;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
//]]>
</script>

I don't know where the event information is stored (maybe the view state) but it seems you would have to add this linkage as well.

Might just be easier to add the button in the HTML from the outright. If that's not possible, maybe try a ListView instead of a div and add buttons to the ListView.

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