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/
Related
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.
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().
I am trying to get the actual HTML text for the control from the client browser because some of the info in this control don't show on the sever side special the "Style". So How can get the whole HTML text for a control?
<asp:TextBox ID="tb1" runat="server" OnChange="tbOnChange();" />
<script type="text/javascript">
function tbOnChange() {
var tb1 = document.getElementById('tb1');
alert(tb1.value);
}
</script>
EDIT: For get style in C#
ASP page:
<input type="text" id="tb1" runat="server" style="color:Red;" />
C# page:
protected void Page_Load(object sender, EventArgs e)
{
var style = tb1.Style;
}
Regards
k.
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.
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.