Execute the function I wrote in C # by changing the value (input) - c#

How can I execute the function I wrote inside the c # code when I change the input value?
<input id="quantity2" runat="server" type="number" onserverclick="lod_gheymat" value="" min="1" max="20" />
protected void lod_Price(object sender, EventArgs e)
{
lbl_Plural.Text = quantity2.Value;
}

Use a asp:Textbox instead of an input control with ‘runat=server’.
Then set the property AutoPostback to true, and handle the event ‘OnTextChanged’.
See: https://meeraacademy.com/textbox-autopostback-and-textchanged-event-asp-net/ for an example.

Related

How to change the name (or value) an specific item on a list-group after selecting the item (Bootstrap 3 / c# / ASP .NET)

I need to know how to modify (Change the name) of an list-group (Bootstrap 3). I have a Text box that will have the new name, and of course the list, after selecting the item or pressing a button the name on the Text box needs to be the new name for the item selected, all of this using the code behind.
<input runat="server" id="Text2" placeholder="New name" class="form-control" type="text" />
<div runat="server" id="List" class="list-group">
Sample 1
Sample 2
Sample 3
Sample 4
Sample 5
Sample 6
Sample 7
</div>
<asp:Button runat="server" ID="Button1" class="btn btn-success" Text="Change Name" />
So far I've tried the following code + added runat="server" and an id to all items in the list.
protected void Page_Load(object sender, EventArgs e)
{
BtnaddBtn.Click += BtnaddBtn_Click;
}
private void BtnaddBtn_Click(object sender, EventArgs e)
{
string test = Text2.Value;
item1.InnerHtml = test;
}
This code does work and is the expected output, however, i don't know how to make it work like this by pressing the item itself.
Any help, suggestions or edits will be much appreciated.

How to get the value of a textbox in code-behind

HTML:
<input type="text" runat="server" value="" placeholder="Search" id="searchB" class="styledTB searchB floatLeft" />
C#:
string strSMain;
protected void Page_Load(object sender, EventArgs e)
{
tbSearchMain = (System.Web.UI.WebControls.TextBox)sender;
strSMail = tbSearchMain.text; // gives me the following error: Exception Details: System.InvalidCastException: Unable to cast object of type 'ASP.site_master' to type 'System.Web.UI.WebControls.TextBox'.
strSMain = searchB.text; //.Text is not an option for me
}
Please help me resolve the issue.
I am creating a web application. And the control is in the MasterPage.
Use this:
<input id="searchB" x:Name="searchB" type="text" runat="server" value="" placeholder="Search" class="styledTB searchB floatLeft" />
The id attribute is used client side. x:Name is used for server side manipulation
I am not sure but no textbox will be sender for Page_Load() function.
Check about DataContext Property and how it works.
http://www.wpf-tutorial.com/data-binding/using-the-datacontext/

hidden variable does not have value after postback asp.net

I have following
<input type="hidden" id="hdnField" name="hdnField"/>
Request.Form.Set("hdnField", x.ToString());
after the page post back the value is not there.
I am new to this, any help would be appreciated.
Source:
You could define a property in your page class and then modify the property value in your code:
protected string HiddenFieldValue { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
HiddenFieldValue = x.ToString();
else
HiddenFieldValue = x.ToString();
}
Then define the hidden form field like this so that it's value is set to the property value:
<input type='hidden' id='hdnField' value='<%=HiddenFieldValue %>' />
If you only want to set the value form the property during a postback or non-postback you could add the condition as well:
<input type='hidden' id='hdnField' value='<% if(IsPostBack) { %> <%=HiddenFieldValue%> <% } %>' />
You should use asp:HiddenField tag provided by asp rather than using the basic HTML input.
<asp:HiddenField ID="hdnField" Value="" runat="server" ClientIDMode="Static" />
Using this, you can read and write value in c# using hdnField.Value and in jQuery using $('#hdnField').val().

How to get Textbox control in C#

I have a HTML textbox
<script>
$(function () {
$("#datepicker").datepicker({
changeMonth: true,
changeYear: true
});
});
</script>
<input type="text" size="10" name="datepicker" id="datepicker" />
Now i need to set the value of textbox to "abcd";
I tried datepicker.Text = "abcd"; Error is "the name datepicker does not exist in current context"...
I tried finding the Control and assigning the value but still could not do it.
Is there any other way to do it??
Thanks
You'll first need to make your <input> control a server side tag, using the runat="Server" attribute:
<input runat="server" type="text" size="10" name="datepicker" id="datepicker" />
Then, you can modify the value using C# code:
protected void Page_Load(object sender, EventArgs e)
{
datepicker.Text = "New Value"; // Initial value for input field
}
If you want to modify the value on the client side, using jQuery, I'd first suggest making the ID static:
<input runat="server" ClientIDMode="Static" type="text" size="10" name="datepicker" id="datepicker" />
Then you can do:
$("#datepicker").val('New Value');
Anywhere after the page has been loaded.

Defaulting hidden field value using inline server code

I have an ASP.NET page where I defined a property in the code behind as follows:
public int testProperty { get; set; }
In my page I define a hidden field and want to set the value using an inline server code as follows:
<asp:HiddenField ID="hftestProperty" runat="server" Value="<%= testProperty.ToString() %>" />
The problem I am having is that when the control renders in the browser, it renders the value the same way I defined it:
<input type="hidden" name="hftestProperty" value="<%= testProperty.ToString() %>">
Any idea why this is happening?
Try with this:
<input id="hftestProperty" type="hidden" value="<%=testProperty.ToString()%>" />
it render:
<input id="hftestProperty" type="hidden" value="0" />
You're going to have to set the value of the hidden field in the code behind, for example in the Page_Load event:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
hftestProperty.Value = testProperty.ToString();
}
}
You might need to cast testProperty to string for hidden field. If you are going to use the string value of testProperty at Server then testProperty.ToString() will always be available to you.
Try following code:
<input type="hidden" id="hftestProperty" value="<%= testProperty%>" />
For more info you may see this question.

Categories

Resources