Get Select2 Selected Value From Code Behind ASP .Net Web Form - c#

I have a problem with Select2 V 4.02
Here is my code
<select id="MySelect" class="form-control" runat="server" ClientIDMode="static">
<option></option>
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
</select>
<asp:Button Text="Show Select2 Result" runat="server" ID="btnShow" OnClick="btnShow_Click"/>
jQuery:
$('#MySelect').select2({
placeholder: "-Select-"
});
My question is:
Can I get the "MySelect" selected value from ASP .Net Code behind?
I tried this code from code behind asp .net webform
protected void btnShow_Click(object sender, EventArgs e)
{
Response.Write(MySelect.Value);
}
But it returns empty string.

you can use standard hidden field like this :
<asp:HiddenField ID="hf1" runat="server" />
then in your JavaScript :
$('#MySelect').on('change', function () {
$('#<%=hf1.ClientID%>').val($(this).val());
}
in your code behind :
protected void btnShow_Click(object sender, EventArgs e)
{
Response.Write(hf1.Value);
}
i hope work for you

Related

How to set a paragraph text dynamically in ASP.NET?

I am creating a website in asp.net. My website has an admin page. The admin will use this page to daily update website's content. This content will get reflected to the main website. I have learned from the following link that how we can pass values from one page to another-
How to pass values across the pages in ASP.net without using Session
I am using Application variable.
Admin.aspx
<form runat="server">
<div>
<asp:TextBox id="DailyMsgID" name = "DailyMsgName" runat="server"></asp:TextBox>
<asp:Button id="b1" Text="Submit" runat="server" OnClick="b1_Click" />
<asp:Label ID="Label_DailyMsgId" name="Label_DailyMsgName" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
Admin.aspx.cs
protected void b1_Click(object sender, EventArgs e)
{
Label_DailyMsgId.Text = DailyMsgID.Text;
Application["DailyMessage"] = Label_DailyMsgId.Text;
}
Home.aspx
<!-- Page content-->
<div id="div1" class="container-fluid">
<h1 id="myhead" class="mt-4">Welcome to the Official Website of ABC</h1>
<p id="DailyMessage"></p>
</div>
To set the paragraph, I want to do something like below. But it is not recognising the paragraph Id.
Home.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
DailyMessage.Text = Application["DailyMessage"].ToString();
}
How do I set the paragraph?
Both Admin and Home page are under the same solution.
This issue got resolved. I was missing
runat="server"
Here is the updated code -
Home.aspx
<p id="DailyMessage" runat="server"></p>
Home.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
DailyMessage.InnerText = Application["DailyMessage"].ToString();
}

Button Click doesn't work to make a H1 visible (asp.net)

I'm new at ASP.NET and web programming in general, here's my issue.
I have this DIV in my .aspx:
<div class="login">
<input type="text" placeholder="Usuário" name="usuario" id="usuario" runat="server"><br>
<input type="password" placeholder="Senha" name="senha" id="senha" runat="server"><br>
<button type="submit" class="btn-sucess" id="btn" runat="server" onserverclick="btn_Click" onclick="btn_Click">Login</button>
<h1 id="lbl1" runat="server" visible="false">Sucess</h1>
</div>
If the user clicks on the submit button it should be visible as it's written here in my aspx.cs
protected void btn_Click(object sender, EventArgs e)
{
lbl1.Visible = true;
}
Nothing happens. Why it doesn't work?
You're setting it to Visible false when rendering the control. Remove the visible property from aspx code and handle it in Page_Load event. All controls need to be inside form.
aspx
<form runat="server">
<div class="login">
<input type="text" placeholder="Usuário" name="usuario" id="usuario" runat="server"><br>
<input type="password" placeholder="Senha" name="senha" id="senha" runat="server"><br>
<button type="submit" class="btn-sucess" id="btn" runat="server" onserverclick="btn_Click" onclick="btn_Click">Login</button>
<h1 id="lbl1" runat="server">Sucess</h1>
</div>
</form>
Code-Behind
protected void Page_Load(object sender, EventArgs e)
{
lbl1.Visible = false;
}
With button click
protected void btn_Click(object sender, EventArgs e)
{
lbl1.Visible = true;
}
Also, if you want to handle anything at a client side, then alone use a javascript function btn_Click. But make sure that returns true and so the form will be submitted to the server.
Something like this,
Javascript
<script type="text/javascript">
function btn_Click()
{
return true;
}
</script>

Access Html TextBox Control in asp Button Click Event?

I have tried this:
Inside AddClass.aspx
<input type="text" id="txtClass" value="ClassName"/>
<asp:Button ID="btnSave" runat="server" Text="Button" OnClick="btnSave_Click" />
Inside AddClass.aspx.cs:
When Button (ID:btnSave) is clicked:
protected void btnSave_Click(object sender, EventArgs e)
{
string a=Request.Form["txtClass"];
}
And I am not getting value in string 'a' .
Is there any way of getting value of html textbox in .cs code.
You need to add name attribute in your text box,
<input type="text" id="txtClass" name="txtClass" value="ClassName"/>
<asp:Button ID="btnSave" runat="server" Text="Button" OnClick="btnSave_Click" />
than you will get the value on submit,
protected void btnSave_Click(object sender, EventArgs e)
{
string a=Request.Form["txtClass"];
}
Try this:
<input type="text" id="txtClass" value="ClassName" runat="server" />
protected void btnSave_Click(object sender, EventArgs e)
{
string a = txtClass.Value;
}
Without the runat="server" attribute being added, the server-side code has no knowledge of this HTML control. Note also, that the property to fetch the content server-side for a HTML text input is "Value", not "Text".

Programatically change value based on selected item from dropdownlist

I'm still getting used to changing over from asp.net to asp.net mvc and I know that it doesn't use on action commands but I'm trying to change the text of a label based on when a user selects an item from a dropdownlist. I'm really not sure where to start :(
You can easily get this done using some jQuery, here I made a Fiddle for you.
Below is how you HTML should look like, incase you are using pure HTML in your views or even if you are using #Html.LabelFor or #Html.DropDownListFor
HTML
<label id="myLabel">Select a fruit:</label>
<select id="fruitSelector">
<option val="">None</option>
<option val="apple">apple</option>
<option val="orange">orange</option>
<option val="mango">mango</option>
</select>
jQuery
$("#fruitSelector").change(function(){
$("#myLabel").text("Fruit has been selected");
});
Related help
https://stackoverflow.com/a/16828702/1182982
https://stackoverflow.com/a/14606324/1182982
Here is the simple example using jquery
#Html.DropDownList("State", ViewBag.StateName as SelectList,
"Select a State",
new { id = "State" })
<label id="lbl1"></label>
<script type="text/jscript">
$(function () {
$('#State').change(function () {
$('#lbl1').text($('#State').val());
});
});
</script>
You can try something like this
<label id="item">Selected Item: </label>
<select id="selector">
<option value="">None</option>
<option value="JS">JavaScript</option>
<option value="aspnet">Asp.net</option>
<option value="mvc">Asp.Net MVC</option>
</select>
<label id="result"></label>
$("#selector").change(function()
{
$("#result").text($(this).val());
});
http://jsfiddle.net/PLbnS/
To start with, your aspx should look like this:
<asp:DropDownList AutoPostBack="true" runat="server" ID="myListDropDown"
CssClass="text" OnSelectedIndexChanged="myListDropDown_Change" />
And your code-behind file:
private void myListDropDown_SelectedIndexChanged(object sender, System.EventArgs e)
{
//put your code here
}
Simple :
Write this code in your SelectedValueChanged Event of dropdownlist :-
private void dropdownlist_SelectedValueChanged(object sender, EventArgs e)
{
dropdownlist.Items["abc"]=label.text;
dropdownlist.Items["xyz"]=label.text;
}

CheckBox control in asp.net (C#)

I added a checkbox to my form.
When the checkbox is checked, some information (TextBox, Label etc.) should appear right under the checkbox.
How can I do this?
Code:
<asp:CheckBox ID="CheckBox1" runat="server"
oncheckedchanged="CheckBox1_CheckedChanged" />
C#:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
}
Don't forget autopostback = true
<asp:CheckBox ID="CheckBox1" runat="server" oncheckedchanged="CheckBox1_CheckedChanged" AutoPostBack="true" />
<asp:Panel runat="server" ID="panel1"></asp:Panel>
-
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
panel1.Controls.Add(new Label { Text = "Text goes here" });
}
This allows you to add any control you want.
Just add TextBox, Label etc. controls and make them invisible.
Then in the CheckBox1_CheckedChanged function make them visible.
This is done by setting the bool Visible property
<asp:CheckBox ID="CheckBox1" runat="server" oncheckedchanged="CheckBox1_CheckedChanged" />
<asp:TextBox ID="textBox" runat="server" Visible=false />
and
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
textBox.Visible = true;
}
Add a new literal control under your checkbox tags,
<asp:Literal id="lblMsg" runat="server"/>
then in your CheckBox1_CheckedChanged event to this:
lblMsg.Text = "Checked";
and yes set the AutoPostBack property of your checkbox to true
I would suggest doing this using javascript. Your users won't have to "postback" and the feeling on the application will be better, and you will reduce the server charge.
Using jQuery, you can use something like this:
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#chk").onclick(function() {
$("#content").toggle();
});
});
</script>
<input type="Checkbox" id="chk"/>
<div id="content" style="display:none">
<asp:TextBox runat="Server" id="oneOfYourControls" />
</div>
jQuery is not mandatory... you can use standard simple getElementById().
The only drawback, is that you cannot dynamically build the content, but in most case it's a not actually a matter
In Checked Changed Event write your code like this:
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
Label1.Text = "Text";
}

Categories