| |

VerySource

 Forgot password?
 Register
Search
View: 829|Reply: 7

Add control dynamically, runat = server?

[Copy link]

2

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 Great Britain

Post time: 2020-1-16 17:20:01
| Show all posts |Read mode
I want to click Mon to dynamically add 4 controls. I originally wrote it in .cs and found that it will disappear after postback, so I put it in html. Now I want to get the value of text0, text1, text3 after clicking button2, but once str + = "<input id = id" + inputNum + "type = text name = text" + inputNum + ">"; becomes str + = " <input id = id "+ inputNum +" type = text runat = server name = text "+ inputNum +"> "; Can I get text0.value in .cs without runat = server?
<script>
var inputNum = 0;
function create ()
{
var str = "";
str + = "<input id = id" + inputNum + "type = text name = text" + inputNum + ">";
inputNum ++;
str + = "<input type = text name = text" + inputNum + ">";
inputNum ++;
str + = "<input type = radio name = text" + inputNum + ">";
inputNum ++;
str + = "<input type = text name = text" + inputNum + "> <br>";
ice.innerHTML = ice.innerHTML + str;
}
</ script>
....
<tr>
<td>
<asp: Label id = "Label3" runat = "server" onclick = "create ()"> Label </ asp: Label>
</ td>
</ tr>
....
<asp: Button id = "Button2" runat = "server" Text = "Button"> </ asp: Button>
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-1-21 11:09:01
| Show all posts
Still have to write in .cs.

postback will disappear. It's because I added! This.IsPostBack. Just remove it.

runat = "Server" is explained by asp.net. It is useless to include runat = "server" in the html generated by the client.
Reply

Use magic Report

2

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-1-25 22:36:01
| Show all posts
According to the meaning of upstairs, it is to click the button to execute the dynamically added code first, and do it again in PAGE_LOAD?
Reply

Use magic Report

0

Threads

58

Posts

32.00

Credits

Newbie

Rank: 1

Credits
32.00

 China

Post time: 2020-1-26 09:45:01
| Show all posts
Dynamically added code must be executed every time.
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-26 19:54:01
| Show all posts
You can consider using JS to dynamically generate controls. And to get its value in the hidden code, you can use hidden to transmit!
Reply

Use magic Report

2

Threads

7

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-1-26 22:27:01
| Show all posts
to: Consider using JS to dynamically generate controls. And to get its value in the hidden code, you can use hidden to transmit!
It turned out that way, but using RESPONSE.FORM [HIDDEN1] produced an error
Reply

Use magic Report

0

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-8-18 22:45:01
| Show all posts
top
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-18 23:00:01
| Show all posts
<script>
var inputNum = 0;
function create()
{
var str = "";
str += "<input id=id"+inputNum+" type=text name=text"+inputNum+">";
inputNum++;
str += "<input type=text name=text"+inputNum+">";
inputNum++;
str += "<input type=radio name=text"+inputNum+">";
inputNum++;
str += "<input type=text name=text"+inputNum+"><br/>";
ice.innerHTML = ice.innerHTML + str;
}
function setvalue()
{
document.getElementById("myHidden").value = document.getElementById("id0").value
}
</script>
....
<tr>
<td>
<asp:Label id="Label3" runat="server" onclick="create()">Label</asp:Label>
<input type=hidden id=myHidden runat=server>
</td>
</tr>
....
<asp:Button id="Button2" runat="server" Text="Button"></asp:Button>

Add when loading in the background:
Button2.Attributes.Add("onclick", "setvalue()");
Get the value in the click event of Button2 in the background:
string text0_Value = this.myHidden.Value;
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list