-5

I want to create 100 buttons in html table (10 row , 10 columns) and want to perform click event. plz help me.

I have tried this...

protected void Page_Load(object sender, EventArgs e) {

    HtmlTable myTable = new HtmlTable();
    for (int i = 1; i <= 100; i++)
    {
        HtmlTableRow row = new HtmlTableRow();
        HtmlTableCell cell1 = new HtmlTableCell();
        cell1.InnerText = "cell" + i;
        row.Controls.Add(cell1);
        HtmlTableCell cell2 = new HtmlTableCell();
        Button btn = new Button();
        btn.Text = i.ToString();
        btn.Click += new EventHandler(btn_Click);
        cell2.Controls.Add(btn);
        row.Controls.Add(cell2);
        myTable.Controls.Add(row);


    }
    PlaceHolder1.Controls.Add(myTable);
}

void btn_Click(object sender, EventArgs e)
{
    Response.Write(((sender) as Button).Text);
}

but it showing OP in single column, i want it in 10x10 or 5x20 like given below

Ajit Singh
  • 15
  • 4
  • 13
  • Welcome to Stack Overflow! Please [take the tour](http://stackoverflow.com/tour) to see how the site works and what questions are on topic here, and edit your question accordingly. See also: [Why is "Can someone help me?" not an actual question?](http://meta.stackoverflow.com/q/284236) – Joe C Jun 18 '17 at 18:08
  • Whats the issue you are having? Please tell us what you have tried so far and which specific part is giving you issues. – CodingYoshi Jun 18 '17 at 18:08
  • i have tried it in another way but i don't want to do this on that way. Actually i'm creating online examination System, let i have a webform which has 100 buttons in table (for 100 questions) , i want to fetch questions by clicking 1 button at a time, so i want it to do use c# , i'm beginner so plz help. – Ajit Singh Jun 18 '17 at 18:26

1 Answers1

-2

As you have tagged asp.net in it:
Do you generate your Html-table in C#? If yes then you just put the <button></button> behind the items you already have in your loop. And then display it on the site like this:

function createHtml(){
            var container = "";
            try{
                container = '@Html.Raw(ViewBag.headline)';
                htmlElement= document.getElementById("yourElementId");
                htmlElement.innerText = container;
            }
            catch(error){...}
}

So you just send it in your ViewBagas another item with your controller.
As always: thanks for silent downvotes. Toxic as always.

So because of the input from the comments:
How to create an HTML button that acts like a link?
Have you tried this?
You can generate the code in your controller, like you (probably) did with your table. Just insert where ever that is something like this:

StringBuilder sb = new StringBuilder();
while(statement){
    sb.append("<td><button onclick="myFunction(row,column)">Click me</button></td>");
}

And then send it in your ViewBag to the view.
At the onclick you define what function should be used in JavaScript. Maybe you should explain the problem a lot more further in your question.

Garamaru
  • 106
  • 1
  • 1
  • 11
  • 1
    Never underestimate the value of a `try`-`catch`-block. – Uwe Keim Jun 18 '17 at 18:08
  • Well sometimes hell breaks loose on places one never expects it... – Garamaru Jun 18 '17 at 18:10
  • 1
    @garamaru if you don't expect it, don't catch it. – CodingYoshi Jun 18 '17 at 18:17
  • @CodingYoshi Try this in large scale applications and you get scolded by your team because you could throw an unhandled exception. – Garamaru Jun 18 '17 at 18:20
  • I did not downvote but people who did, did it for the obvious reason that you are not answering the question. You are not creating a table, there are no buttons, there are no event handlers for the click of the button. There is no controller in asp.net. So your answer will not help the OP at all. – CodingYoshi Jun 18 '17 at 18:21
  • @garamaru I will defend myself if the team scolds me. What makes you think I have not tried it in a large scale application – CodingYoshi Jun 18 '17 at 18:23
  • Oh boy...where do I start...So OP basically just generated an HTML table in the view, as far as it reads - so he only needs to include the button element with the correct properties. If you have a good answer for him then post it. And don't just comment. How do you defend it, if some important HTML code isn't transferred to the view and there is no tracking of the failure? Would be kind of interesting and that's why I assumed that you don't have an experience in team projects. But anyway, it doesn't help OP. – Garamaru Jun 18 '17 at 18:34
  • i'm new here , so can anyone tell that where do i post my codes that i have tried.... – Ajit Singh Jun 19 '17 at 18:51
  • I can't post it in comment bcoz it is long.. – Ajit Singh Jun 19 '17 at 18:51
  • @AjitSingh Just post the code in your question (via edit) and there you have a button that looks like this: "{ }" (or curly braces / right Alt + 7 , 0). Then your code gets formatted correctly in the question. – Garamaru Jun 19 '17 at 19:13
  • 1
    Thanks @Garamaru – Ajit Singh Jun 19 '17 at 19:44
  • thanks to all for giving me solutions. – Ajit Singh Mar 12 '18 at 14:52