I have a template field in which I had a label and a hiddenfield. But when I tried to access the hiddenfield value using findcontrol I got a NULL Exception error. But when I put the seperate template field and use the hiddenfield then I could get the value.
I dont want to show the template at the same time want the value from hiddenfield. How am I supposed to proceed?
Just use a label with style="display:none;" inside any of the template fields. Doesn't matter if there is any other control along with it. As long as you don't mind users using developer tools to see it, you are good to go. If you don't want it to be there in the page at all, just set visible="false".
Depending on when you want to retrieve the hidden field the code should be something along the lines of this:
var hf = GridView1.Rows[e.RowIndex].FindControl("hiddenFieldId") as HiddenField;
If you post more code I can add more information.
Related
I've creating one ASP web application, in that application one form have to update date value in the textbox field i put calender button near that textbox. it can update the date in that textbox field but its editable . i want update date value through only using calender button , i used read-only property but return empty value so not works .
Try client side html readonly attribute instead of ASP.NET server side readonly.
myTextBox.Attributes.Add("readonly", "readonly");
From MSDN,
The Text value of a TextBox control with the ReadOnly property set to true is sent to the server when a postback occurs, but the server does no processing for a read-only text box. This prevents a malicious user from changing a Text value that is read-only. The value of the Text property is preserved in the view state between postbacks unless modified by server-side code.
This is why textbox with server side readonly attribute has null value in postback.
You can use either
TextBox1.Enabled = false; OR
TextBox1.Attributes.Add("readonly","readonly");
Difference is that if you make enabled= false then you cant pass the value of the textbox. If you need to pass the value of the textbox then you should use read-only property of textbox.
On page load event of your code behind file just write following code:
yourTextBoxName.Attributes.Add("readonly","readonly");
you can also do same by using property
Enabled="false"
on your aspx page but then you will not be able access its value in code behind page.
You can make textBox non-editable in asp.net
By using read only it will give problems on post back just set this java script property
onkeydown="javascript:return false"
by using this u can have properties like readonly and have absolutely no problem will araise
You might want to use this:
TextBox1.Enabled = false;
I am hiding a TextBoxin my aspx page like this:
myField.visible=false;
Now I have a DropDown as well which tries to access the TextBox on IndexChange. The problem is, it cant access the hiffen TextBox and I am getting document.form[0] is Null or Not an Object. How can I solve that? Is the some check for that in JavaScript?
Thanks :-)
If you are trying to access the first form in the document via its numerical index (hint: You shouldn't be, the id attribute is very handy, then you are looking for the first in a group of forms, not the first in a group of form.
document.forms[0] // It is plural
I am using ASP .NET (C#) and have a page with a listview linked to a sqldatasource.
The listview has a InsertItemTemplate which contains many textboxes. I want to make all the textboxes required via the RequiredFieldValidator (and the ValidatorCallOutExtender).
Is there a way to do this in the codebehind instead of the aspx page?
Ideally I would like the page to validate each control with the same method with only the error message changing.
I take it your question is about dynamically adding controls (and not about codebehind validation).
I didn't know and ran a few tests. It is possible to simply add (Validator)controls to the Controls property of the Form element, but controlling the order is not so simple (no Controls.Insert()).
So my advice would be not to use a plain TextBox but a UserControl. You could probably use Search&Replace to fix up the ItemTemplate.
I want to change the font size of my gridview in javascript because i am creating a printable version. How can I change the font size in javascript for the gridview?
With a gridview named GridView1 some javascript code like:
document.getElementById('<%=GridView1.ClientID%>').style.fontSize = "12px";
There are a lot of ways to do it, but the method I would use if I were to attempt it is to get the unique ClientID of each control (label, TextBox, literal) in the gridview that I wanted to change and as it was going through the RowCreated event, I would inject the ClientID into an array of control names. Then when the action in JavaScript is executed, I'd simply need to traverse the array and set the style for each control. Using jQuery would even speed this up.
I have created my own control which contains TextBox control as a child control. this textbox has autopostback set on. when the postback is called I dont get any text in request.querrystring.
where should I specify text entered into texbox will be send to response ? or is there any easier way of sending this information? I want to have the entered text once again in textbox after postback.
thank you
For dynamically created controls, you need to create them and add them to the control tree in the Page_Init() event; Page_Load() is too late, and Page_PreRender() is way too late.
Once in the control tree, the runtime will restore the state of the control for you after a postback. You can access the contents of the control using myTextBox.Text -- you don't need to use Request.QueryString
For a control that only contains a TextBox, things would be much easier if you used a user control rather than an INamingContainer. That way, you could just use <asp:TextBox> in your .ascx markup.
A custom control? Any posted control like this loads its data in LoadPostData method. You can override this in your custom control class, and access the value from teh control via:
var text = postDataCollection[textboxID];
The textbox within your custom control has to have an ID.
If you are talking something else, let me know the specifics.
Thanks.
(TextBox)Control.FindControl("control id")
get dynamic control's value in postback , Request.Form("control client id")