I have:
<input name="input4" type="text" id="input4" disabled="disabled" class="jsx-3771882255" data-id="0">
I want get data of data-id by C#;
string s=input4.Attributes["data-id"].ToString(); //(can'tuse this!)
You have the disabled attribute set on your text box, this will not get submitted to the server, use the readonly attribute instead of the disabled attribute.
#vuong quang - Can you check my code and implement it's working for me and also I shared my code with a screenshot.
HTML Code
Add runat="server"
<input type="text" name="name" value="0" data-id="101" runat="server" id="txtvalue" />
HTML Code
Code behind
string id = txtvalue.Attributes["data-id"].ToString();
Check below screenshots
CS Page Code
Related
I have an ASP.NET site and need to post some hidden form fields to SagePay so that my customers can pay for goods. I am using the following method to do this:
<input type="hidden" name="VPSProtocol" value="2.23" />
<input type="hidden" name="Currency" value="gbp" />
<input type="hidden" name="TxType" value="PAYMENT" />
<input type="hidden" name="Vendor" value="myvendorname" />
<input type="hidden" runat="server" id="crypt" name="Crypt" value="#<encrypted string>" />
<asp:Button ID="Button1" runat="server" Text="Pay Now" PostBackUrl="https://live.sagepay.com/gateway/service/vspform-register.vsp"/>
Now, If I use this code in a standard ASP.NET form, this works fine - SagePay accepts the posted information and continues with the payment process. However, if I use the same code inside a content page with a master page, Sagepay displays the following error screen:
5030 : We could not process your message, please check your
integration settings or contact the support team.
It seems as if the hidden fields are losing their value because of the master page.
Could anyone tell me what could be happening here and if there is anything I can do to rectify the situation. I need to use the SagePay Form method and I need to use a masterpage.
I haven't used webforms for a while but from memory by default it changes the names of your elements based on their container to allow navigation and identification server side: MSDN documentation here.
This means that your posted values are not under the name you expect them to be.
In my ASP .Net Form application I need to post some data using hiddenfields.
Need to set the values dynamically in page load in the code behind file
Have to use the hidden fileds in a web form which using a masterpage.
I have to add runat="server" attribute as need to access the field in code behind file to assign value dynamically..... There the problem begins.
eg:
<input type="hidden" runat="server" id="uname" value="abc" />
converts to following by ASP .Net in run time
<input name="ctl00$content$uname" type="hidden" id="content_uname" value="abc" />
So a diffrent filed name="ctl00$content$uname" gets posted.
I tried adding ClientIDMode="Static" but still a different named field creates by ASP .Net in run time for name field as following
<asp:HiddenField ID="uname" runat="server" Value="abc" ClientIDMode="Static" />
Converts to following by asp .net
<input type="hidden" name="ctl00$content$uname" id="uname" value="abc" />
If somebody can guide me of how to post values using hiddenfields by assingning values in run time in code beghind in a masterpage environment in ASP .Net, would be really grateful. Thanks...
Any time you use runat="server" you essentially give WebForms control over that, well, "control". Which means that WebForms is going to dictate the resulting markup. Since you need granular control over the markup, you need to create it manually:
<input type="hidden" name="uname" />
Since this is just plain HTML, the WebForms rendering engine won't modify it. Then to assign a value to this from server-side code, you'd use an inline server-side statement:
<input type="hidden" name="uname" value="<%= SomePageMember %>" />
In this case, SomePageMember is a public or protected class member for the page's class. Something like this:
protected string SomePageMember { get; set; }
This will allow the UI page (which inherits from the code-behind class) to inject that value directly in the markup, while still giving you granular control over the markup itself.
You could have the value set via a server tag and not user runat="server"
<input type="hidden" runat="server" id="uname" name="uname" value="<%= HiddenValue %>" />
Create a global variable named HiddenValue and set the value when the page loads.
Edit: Just realized that this the same advice as #Bartdude.
I have a JavaScript function as follows:
function getWindowSize() {
test.value = $(window).width();
test2.value = $(window).height();
}
window.onresize = getWindowSize;
And in the ASP.NET page:
<input type="text" id="test2" value="Test"/>
<input type="text" id="test" value="Test"/>
This populates the fields when the screen size changes. When I put the page elements to have runat="server" they don't populate the screen size and cannot capture it server side.
Any suggestions?
You need to use ClientID if you have server tag. You are not currently using getElementById but you should to get the DOM element for better browser compatibility. If you have framework 4 and above you can use ClientIDMode and do it without ClientID
Javascript
function getWindowSize() {
document.getElementById('<%= test.ClientID %>').value = $(window).width();
document.getElementById('<%= test2.ClientID %>').value = $(window).height();
}
window.onresize = getWindowSize;
HTML
<input type="text" id="test2" value="Test" runat="server" />
<input type="text" id="test" value="Test" runat="server" />
Since I cannot see the full code, I am going to assume ASP.NET replaces Id attributes with generated ones (something like _ctl$Container$test). As a workaround, if you use jQuery you can use CSS classes:
Javascript
function getWindowSize() {
$('.WidthValue') = $(window).width();
$('.HeightValue') = $(window).height();
}
HTML
<input type="text" id="test" value="Test" class="WidthValue" runat="server" />
<input type="text" id="test2" value="Test" class="HeightValue" runat="server" />
Are you using Masterpages? Because when adding ASP.NET form controls using masterpages they use a prefix to identity the controls:
<input id="ctl00_ContentPlaceHolder1_txtName" class="textfield" type="text" name="ctl00$ContentPlaceHolder1$txtName">
Where ctl00_ContentPlaceHolder1_ is the prefix of the masterpage.
This can be the cause why your javascript function doesn't populate the fields.
How do you identify your objects test and test2?
I want to get the value of my input tag into my C#.
<div id="datetimepicker2" class="input-append">
<input data-format="MM/dd/yyyy HH:mm:ss PP" type="text"></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar">
</i>
</span>
</div>// what should we use here?
The value is set by the user of the page.
I did't understand which control value you want to get. But If you want to get input element value into the code behind, you need to set runat="server" attribute, because this is a simple html element not a Asp control.
Add runat="server" and id="your_id" and you should have access to them.
for example:
<input type="text" value="Username" class="input-text autoclear"
runat="server" id="myTextBox" />
than you can simply get value of input box like this:
string myStringFromTheInput = myTextBox.Value;
For more options please See here
Try this
Add name for your input type
<input data-format="MM/dd/yyyy HH:mm:ss PP" name="txtBox1" type="text"></input>
and try this way for get value in codebehind
string value=Request.Form["txtBox1"];
You can access all your submitted form data at server side by looking into the form Request object.
Ex. Request.Form["txtDate"] OR Request["txtDate"].
Naming the html elements makes easier to look into form collection for specified element.
If what you posted is your actual code, you have an extra space in your closing tag
</asp: TextBox>
Should be
</asp:TextBox>
and then txt_todata.text should work
To pass values from javascript to the code behind after a postback I use this code:
string strRowNumberTblOne = Request.Form["iRowNumberTblOne"];
<input type="hidden" id="iRowNumberTblOne" name="iRowNumberTblOne" value="" />
Is there a way to clear the input field from the code behind?
The Request.Form is read only.
Add runat="server". Then set its Value property.
Try this Instead of you can use Hidden TextBox , like this
<asp:TextBox ID="TextBox2" style="display:none" runat="server"></asp:TextBox>
In JavaScript
varResult = document.getElementById('<%= TextBox2.ClientID%>');