简体   繁体   中英

ASP adding button on a table dynamically, don't working onserverclick

Have a nice day. I try to create a aspx file that generate html table with data from DB. Html table contane 4 columns (first - number of rows, second column contain same value in rows, so i use rowspan() to join rows, third some value, fourth column - button)

Create table

foreach (var group in grouped)
               {
                   j++;
                   int k = 1;
                   group.Count().ToString();
            %>


                <% 
                   Response.Write("<tr><td rowspan=" + group.Count().ToString() + "\">" + j + "</td>");
                   Response.Write("<td rowspan=" + group.Count().ToString() + "\">" + group.Key.ToString() + "</td>");
                    foreach (var innerlist in group)
                   {
                       if (k == 1)
                       {
                           Response.Write("<td>" + innerlist[0] + "</td>" );                  
                       } 

                       if (k != 1)
                       {
                           Response.Write("<tr>" + "<td>" + innerlist[0] + "</td>" );
                       }
                       Response.Write("<td > <button id=\"some_id1\" onserverclick=\"doit\" runat="server" >  кнопка </button> </td> </tr>"); 
                       k++;

                   }

in script section i create

<script runat="server">
protected void doit(Object sender, EventArgs e)
        {
            ViewState["count"] = Convert.ToInt32(ViewState["count"]) + 1;
            Span1.InnerHtml = "You clicked Button1 " + ViewState["count"].ToString();
        }

</script>

When i click on button in html nothing happend, whats wrong? ViewState contains in Page_Load (see below)

Read data from DB i try add to Page_Load, but array_list_result not available in the tag body. How to fix it?

protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                ViewState["Count"] = 0;


                List<List<string>> array_list_result = new List<List<string>>();
                DataObjectList LotPhisParam = Session.Land_LotPhisParameters.Query("<Object/>", "ObjectID <> '' AND ObjectID IS NOT NULL");
                // LotPhisParam.ToArray().GroupBy;
                for (Int32 i = 0; i < LotPhisParam.Count; i++)
                {
                    List<string> temp_string = new List<string> { LotPhisParam[i].Id.ToString(), LotPhisParam[i].GetLink("Object").Id.ToString() };
                    array_list_result.Add(temp_string);

                    // LotPhisParam[i].Delete();
                    // Session.Commit();

                }

            }
        }

Its better you write your tags to a parent html element. Say a div that you can access from the server side.

Don't use Response.Write

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