I have an asp.net form.
But the controls inside the form at 1 textbox and 2 dropdown lists as a row.
And there is a "plus" and "minus" buttons for users to add in and delete the rows.
When the form is submitted, I will grab the values from those controls by using Request.Form["ControlName"]
But I need to confirm whether that ["ControlName"] exists.
I can put that piece of code in try catch to confirm like this
for(int a=1;a<10;a++)
{
try
{
Response.Write(Request.Form["ControlName"+a.ToString()]);
}
catch {}
}
By doing this, the controls which don't exist will be catched by catch statement in theory.
But I am trying to use another method to do the checking like FindControl("ServerControlID")
But that one is for the server controls only.
My front code will be something like this
<input type="text" id="txt1" name="txt1"/>
<input type="text" id="txt2" name="txt2"/>
<input type="text" id="txt4" name="txt3"/>
NOTE : I cannot add in runat="server". If so, I can use FindControl()
If you want to access a control on server side (code behind), than that control must be a server control or even an html control but with runat = "server" attribute, by introducing you can access the HTML control.
<input type="text" id="txt1" name="txt1" runat = "server"/>
You could use the NameValueCollection returned by Request.Form.AllKeys.
This returns an IEnumerable
Use Linq to check it as follows:
for(int a=1;a<10;a++)
{
var paramName = "ControlName"+a.ToString();
if(Request.Form.AllKeys.Contains(paramName ))
{
Response.Write(Request.Form[paramName ]);
}
else
{
//key not present
}
}
Related
HI All - PLease refer the attached screen shot. The status and Role fields are enclosed in a panel for each check box.
On click of the submit button, only data for the selected check box should be picked up. Can you please help with a pseudo code how the data (Status and Role) for the first and third checkbox can be retrieved.
These controls are kept in a table. No grid view is being used.
Thanks for the help.
Yagya
All data will be returned to the server, but that's not a problem, just act on whatever the spec says.
I assume that the checkboxes are hardcoded <asp:Checkbox /> or <input type="checkbox" runat="server" /> controls, then it's simple:
Assuming <input type="checkbox" runat="server" id="reporting" /> then:
public override void OnLoad(Object sender, EventArgs e) {
if( this.reporting.Checked ) {
String status = this.reportingStatus.Value;
String role = this.reportingRole.Value;
// your logic here
} else if( this.assignment.Checked ) {
// etc
} else... // and so on
}
I have asp.net page and it's contain Text boxes ,Drop down lists ,check boxes and grids
the question is :
When i click on save Button , how can I clear All data that entered and selected in this page ?
Your can do this, with a little bit of javascript. Using a framework such as jquery, it makes it very easy to find all type of input and clear there value. Example given below.
HTML:
First name: <input type="text" id="firstname" /><br />
Last name: <input type="text" id="lastname" /><br />
<button>Submit</button>
JavaScript:
$("button").click(function () {
$("input").val('');
});
More Info
TextBox1.Text = String.Empty;
DropDownList1.SelectedIndex = -1;
...
If you want to get fancy you can iterate through all of the controls and containers on the page and set their values to some default.
Of course, a quick way would be to simply issue a response.Redirect back to the same page. This is essentially telling the browser to start over. Presumably all of the values are blank to begin with.
Another approach is to use the built-in Controls.Clear() method on your form:
form1.Controls.Clear()
You can do something like this
public static void ClearControls(Control Parent)
{
if (Parent is TextBox)
{
(Parent as TextBox).Text = string.Empty;
}
else if (Parent is DropDownList)
{
(Parent as DropDownList).SelectedIndex = 0;
}
else
{
foreach (Control c in Parent.Controls)
ClearControls(c);
}
}
and call following code where you want
ClearControls(Page);
I am working on a web forms page which has a GridView which contains two columns of radio buttons generated from an asp:RadioButton control.
I wanted to implement a "select all" checkbox in the respective column headers that would when checked select all of the corresponding radio buttons to checked in that particular column.
I wrote a small bit of jQuery which would do this but it didn't work straight away. In order for jQuery to select each radio button and mark it as checked I set the class name of the asp:RadioButton control using the CssClass attribute:
<asp:RadioButton ID="id" CssClass="myClass" runat="server" ... />
I was expecting this to generate markup something like:
<input type="radio" class="myClass" ... />
Meaning my jQuery selector would be:
jQuery("input.myClass")
Instead it has wrapped the input element in a span element and applied the class attribute I specified to the span element instead of the input element.
Is there a way to prevent ASP.NET generating this wrapping span element around my input element when using the asp:RadioButton control?
Can I get it to apply the class attribute to the actual input element, instead of the wrapping span element if ASP.NET has to generate it?
(Note: I have updated my jQuery to use a selector that works in the meantime:
jQuery("span.myClass input")
)
Web controls in the System.Web.UI.WebControls namespace may render differently in different browsers. You can't count on them rendering the same elements always. They may add anything that they think is needed to make it work in the specific browser, changing with each version of .NET.
If you want to have any control over how the controls are rendered as html, you should use the controls in the System.Web.UI.HtmlControls namespace instead. That is:
<input type="radio" id="RadioButton1" runat="server" class="myClass" />
<input type="radio" id="RadioButton2" runat="server" class="myClass" />
<input type="radio" id="RadioButton3" runat="server" class="myClass" />
They will render just as the corresponding html element, with no extra elements added. This of course means that you will have to take responsibility for the browser compatibility, as the control doesn't. Also, those controls doesn't have all the features of the controls in the WebControls namespace. So it depends on your needs for the specific situation.
You could also find another means of selecting all of the inputs in jquery, like basing it on the id (using a similar name on all of them, and a wildcard to select them all). Attribute Contains Selector
This is one of the complaints of ASP.NET WebForms is you don't get absolute control over the rendered HTML. I have used jQuery selectors exactly as you have implemented and works just fine.
You can't change the way the asp.net render it's control so you can remove the asp.net radio button and user the input tag instead with runat="server".
You can create a new ASP.Net Server Control and change inherit from WebControl to RadioButton
Then modify html after it's renderd to this. It removes the span tag and moves attributes Class And Title back from the span to the rado button input.
public class MYRADIOBUTTON: RadioButton
{
public bool AddSpanTag { get { return ViewState["AddSpanTag"] != null ? (bool)ViewState["AddSpanTag"] : false; } set { ViewState["AddSpanTag"] = value; } }
protected override void Render(HtmlTextWriter writer)
{
if (AddSpanTag)
{
base.Render(writer);
}
else
{
StringWriter w = new StringWriter();
HtmlTextWriter rbw = new HtmlTextWriter(w);
base.Render(rbw);
rbw.Close();
string html = w.GetStringBuilder().ToString();
if (html.Contains("<span"))
{
int start = html.IndexOf("<input");
int end = html.IndexOf("/>", start);
string rbHtml = html.Substring(start, (end - start));
if (CssClass != "")
rbHtml = rbHtml + " class=\"" + CssClass + "\"";
if (ToolTip != "")
rbHtml = rbHtml + " title=\"" + ToolTip + "\"";
html = rbHtml + "/>";
}
writer.Write(html);
}
}
After you build this in your webproject. Open a page in Designview then you should get a new Tab in Toolbox with your custom radiobutton control . Drag it to the page and set the Property AddSpanTag to false.
<cc1:MYRADIOBUTTON ID="btnSel" ToolTip="Select" GroupName="select" AddSpanTag="false" runat="server" />
How can I get gridview HTML textbox value in .aspx.cs code?? E.g. :
<input id="Frequency" name="customerName" type="text" style="width: 44px" />
If i use the bellow code ,Then i can get the value on selectedIndex event.
string n = String.Format("{0}", Request.QueryString['customerName']);
I want to use bellow syntax.
TextBox_Label1 = (TextBox)e.Row.FindControl("Frequency");
i don't want to user the runat="server" on HTML control .
From Gridview i need to call a popup,Popup return a value ,I use the bellow code on javascript to do that
window.opener.document.getElementById("customerName").value = val;
window.close();
In my gridview .if i put the runat="server" then return value not set ,So i need to remove the runat="server".It also not work if i put the Asp:TextBox on Grid.Help me to Return popup value on gridview Asp:TextBox
Thanks!
Try a databinding expression:
<input id="Frequency" name="customerName" type="text" style="width: 44px"><%# String.Format("{0}", Request.QueryString["customerName"])%></input>
If you're having problems with this process and the difference is one is runat="server" and the other is not, I would suggest you need to look at your JavaScript to make sure that you have the proper element selection method. The rendered ClientID will be different from a standard html control ID. If you write your code so that the ClientID is injected into the JavaScript, you can keep the runat="server" and achieve your results. Standard html controls are not accessible from the code behind.
I have a textbox which is extended by an Ajax Control Toolkit calendar.
I want to make it so that the user cannot edit the textbox and will have to instead use the calendar extender for input.
I have managed to block all keys except backspace!
This is what I have so far:
<asp:TextBox ID="TextBox1" runat="server" onKeyPress="javascript: return false;" onKeyDown="javascript: return false;" onPaste="javascript: return false;" />
How would I also disable backspace within the textbox using javascript?
EDIT
Made an edit since I need a solution in javascript.
EDIT
It turns out that onKeyDown="javascript: return false;" DOES work. I have no idea why it wasn't working before. I tried using a new textbox and it blocked backspaces fine. So sorry to everyone who posted an answer hoping to get some rep esp. after I marked it for bounty.
My textboxes now (seem) to block ALL keystrokes and also still work with the calendar extender.
ZX12R was close. This is the correct solution:
The TextBox is like this:
<asp:TextBox ID="TextBox1" runat="server" onKeyDown="preventBackspace();"></asp:TextBox>
and the script looks like this:
<script type="text/javascript">
function preventBackspace(e) {
var evt = e || window.event;
if (evt) {
var keyCode = evt.charCode || evt.keyCode;
if (keyCode === 8) {
if (evt.preventDefault) {
evt.preventDefault();
} else {
evt.returnValue = false;
}
}
}
}
</script>
First of all, the backspace wont come through on Key Press, so you have to use Key Down.
Can't you just use the HTML readonly="readonly" attribute?
<input type="text" name="country" value="Norway" readonly="readonly" />
<textarea rows="3" cols="25" readonly="readonly">
It should work! :)
</textarea>
How about using a label for the display and a hidden textbox to get the value back to the server?
You need to apply readonly on the client side controller ONLY, so that asp.net doesn't see it and still reads the data on postback. You can do this several ways, one of the easier if you use jQuery is to add a class to the text-boxes eg. cssclass="readonly" in question and $(".readonly").attr("readonly", true);.
As others said ReadOnly="True" will break the postback mechanism.
I believe you can get around it in your code-behind by accessing the Request object directly during PageLoad:
//assuming your textbox ID is 'txtDate'
if(Page.IsPostBack)
{
this.txtDate.Text = Request[this.txtDate.UniqueID];
}
Your other option is to allow Disabled controls to postback on the form, but this is somewhat of a security concern as readonly fields modified via script could potentially come back:
<form id="MyForm" runat="server" SubmitDisabledControls="True">
..
</form>
http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.submitdisabledcontrols.aspx
I'm not sure the impact of this property on ReadOnly (vs Enabled="False") controls but it's worth trying.
And finally - I did run into the same issue you're having a few years ago, and from what I remember there is a difference between using an html input marked as readonly and runat="server", and an actual serverside control where ReadOnly="true".
I have a feeling doing:
<input type="text" readonly="readonly" runat="server" id="myTextBox" />
may have still allowed the data to come through, although in the code-behind you have to treat the control as a HtmlInputText or HtmlGenericControl vs. a TextBox. You can still access the properties you need though.
Just a few ideas anyway...
here is a possible solution... add an event listener...
<asp:TextBox ID="TextBox1" runat="server" onKeyPress="KeyCheck;" />
and then the function can be like this..
function KeyCheck(e) {
var KeyID = (window.event) ? event.keyCode : e.keyCode;
if (KeyID == 8 ) {
alert('no backspaces!!);
}
}
doubt if it has to be onkeypress or onkeyup...
ReadOnly attribute does not help. The backspace still is taking your browser to back page even if your text box is read only..
use regular text boxes not read-only and not Disabled, just use client-side JavaScript to ignore keypresses.
This will ignore all keypress so you will have your READONLY behaviour and it will also ignore the backspace.
<input type="text" value="Your Text Here" onkeydown="return false;" />
No Need to call any function and all just try this:
<asp:TextBox runat="server" ID="txt" onkeydown="return false;"
onpaste = "return false;" onkeypress="return false;" />
I was able to do something similar, with Jquery. Just putting it out here for reference!
$("#inputID").keypress(function (e)
{e.preventDefault();});
$("#inputID").keydown(function (e)
{e.preventDefault();});
the first prevents keypresses, while the second prevents key down events.
Still a JS noob, so feel free to point out good / bad things with this.