Checkbox not working in asp.net and HTML mixed page - c#

I'm grappling with an issue where the site is asp.net/C# but controls on the .aspx pages are HTML and I'm not sure how well it would go over if I would change everything into asp.net controls. Also the change is minor. I was tasked to add a check box, as in <input type="checkbox" name="disableFeatureManager" runat="server" id="disableFeatureManager" />Disable Feature Manager and in the .cs page I want to check if the box is checked and make decisions based on that, but the control's checked property is always false. The submit button is also a HTML control: <input type="submit" value="Start" name="submitButton" />
In the Page_Load ckecking if check like this returns false.
if (disableFeatureManager != null && disableFeatureManager.Checked)
nextURL.Append(FeatureManagerChoices.CreateQueryStringFromFormData(Request.Form));

You could keep your checkbox as an Html server control by doing the following:
<input type="checkbox" name="disableFeatureManager" runat="server" id="disableFeatureManager" />
Then you could change your button to a web control as follows:
<asp:Button ID="submitStart" runat="server" OnClick="btn1_Click" Text="Start" ClientIDMode="Static" />
The only difference with the above rendered HTML will be the name that is output but you will have an Id that is submitStart due to the ClientIdMode being static, if you need a friendly consistent Id for javascript manipulation.
To wire in the event add this code which will read the value from the checkbox:
protected void btn1_Click(object sender, EventArgs e)
{
var isChecked = disableFeatureManager.Checked;
}

As I mentioned before, I was trown into the deep end of MVC. Co-worker offered a simple answer(as least in this case):
bool isChecked = false;
if(Request["disableFeatureManager"] != null)
isChecked = (Request["disableFeatureManager"].ToLower() == "on");
if (!isChecked)
nextURL.Append(FeatureManagerChoices.CreateQueryStringFromFormData(Request.Form));
I think this can be further simplified in to one IF statement.

Related

Can Asp:Label Value be manipulated by user when the label is set as hidden?

I have an application where i use ASP labels/inputs and set their visibility to hidden to store critical values.
<asp:Label ID="userid" runat="server" Text="" Visible="false">
</asp:Label>
As expected these labels dont show up directly on the webpage but instead div like the one below is placed in the webpage:
<div class="aspNetHidden">
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="">
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="n6XWzGQDXHjDGVPZuZa....">
</div>
I wanted to ask if the user can change these values and manipulate or find the actual value for such labels and inputs!
Thanks!
Actually, no they can't change the label text. (assuming visible = false).
Remember, if you set a control. Visible = false, then in fact the .net system does NOT send that control down to the client side.
In other words, say we do this:
simple markup:
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="My label text"
ClientIDMode="Static"></asp:Label>
</div>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<br />
<asp:Button ID="Button2" runat="server" Text="Hide the label"
OnClientClick="MyHideToggle();return false;"
/>
<script>
function MyHideToggle() {
// hide the label, or show it if hidden
var MyLabel = $('#Label1')
if (MyLabel.is(":visible"))
MyLabel.hide()
else
MyLabel.show()
}
</script>
</form>
and now say we have this server side code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Label1.Visible = false;
//Label1.Text = "super zoo";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Debug.Print(Label1.Text);
}
Ok, so on page load, we set the label text. With above code, our JavaScript toggle code works fine. and the value we set the label to (on first page load) WILL persist.
but, what happens now if we do this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label1.Visible = false;
Label1.Text = "super zoo";
}
}
Ok, so this means:
We did set a value of the Lable - IT WILL persit in view state.
But, our js code can't work now. When you set Lable1.Visbile = false, then it DOES NOT get renderd on the client side.
So, the js code can't work.
But, you WILL see the button click to display the label text (what we changed) text DOES work.
How can this be?
Well, in this case? That encrypted blob and mess you see for view state?
Well, view state is STILL maintained for controls - EVEN ones that don't get rendered and sent to the client side.
So, can they change the text of a label that is visible = false, and not rendered?
Well, for all practical purposes? No, Unless we assume that the encrypted view state has be cracked.
so possible? Perhaps.
So , for the most part practical? No, not really practical.
So, a text box, or label? They do have automatic state management, and it goes into that encrypted view state blob. And that IS SENT to the client side.
But, do keep in mind the ramifications of controls not being sent.
For example, if I want to hide the label, but STILL want the JavaScript (client side code) to work?
Then I can't use visible = false, but have to hide the control with style, and use display:normal, or display:none, say like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label1.Style.Add("display", "none");
Label1.Text = "super zoo";
}
}
Now, the control WILL be hidden, will be sent to the client side. At this point, yes, someone can rather quite easy mess or change the label text.
I think as a general approach? If you hide a control, (visible = false), then you are secure enough for most use cases. And if you do hide a control, then you will NOT see the control in the markup, nor will it be sent to client side. And thus as noted, any JavaScript client code cannot work, nor operate on that control.
But, the label "text" we modified does in fact exist in the client side browser - and it exists in the encrypted View State blob that you noted in your post.
but, if you term "hidden" means only a style display:none, and NOT some server (code behind) setting of Visible = False? Then yes, that control and label and text can most certainly be modified by client side code - be it JavaScript, or a user flipping their browsers into debug mode.
So the "way" you hide that control will change what the user can and cannot do. Using style display:none (which is what jQuery does for hide() and show(), then the control still exists in plane view in the pages markup. And even in this case - you can't see it, but flipping into the page source in the browser - the control will be in plain view - and yes, it can be changed by the user - will relative ease and skill.

Why is there no PostBack for a form on a UserControl?

I have a problem understanding where I am losing the ability to catch the PostBack trigger or simply the handler method of serverclick in this setup and placing breakpoints only shows the PostBack occurs on rendering (Page_Load) but not after submit button is clicked. Allow me to elaborate on the scenario and keep in mind I have limited ability to change the way it is done and must figure out a way to make it work with minor changes using ASP.net / C# / WebForms / Bootstrap 4.5 per the client.
Dashboard.aspx [main page] has a simple (top nav showing the logo and logged in user's name with a signout drop down, sidebar menu which upon individual click will load into a display area) and the display area is an asp:PlaceHolder element:
<asp:PlaceHolder runat="server" ClientIDMode="Static" ID="TheScreen">
When the [main page] is loaded, it makes an API call and processes a collection of objects received and generates adding a series of UserControl Control1 objects (this can be zero items or n items) let's call this [default state] of the landing so we can refer to it later.
Each of the Control1 (which have unique IDs) have 3 buttons to perform 3 different actions.
<a class="btn btn-primary btn-sm" id="action1" runat="server" onserverclick="Command_Click">Do First Action</a>
<a class="btn btn-primary btn-sm" id="action2" runat="server" onserverclick="Command_Click">Do Second Action</a>
<a class="btn btn-primary btn-sm" id="action3" runat="server" onserverclick="Command_Click">Do Third Action</a>
Command_Click simply bubbles up an event handler to be processed by Dashboard.aspx
public void Command_Click(object sender, EventArgs e)
{
CommandClicked?.Invoke(sender, e);
}
action1 is handled on the fly with a confirmation modal which contains a simple button that runs onserverclick and reloads the [default state] having deleted that item using an API call.
<button type="button" class="btn" id="do-action1" runat="server" onserverclick="finish-action1">Do It</button>
This all works as expected.
action2 is handled by [main page] where it clears the PlaceHolder's controls and then dynamically creates and loads another UserControl which displays long form details of that object.
UControl2 theObject = (UControl2)Page.LoadControl("~/path/to/Control2.ascx");
TheScreen.Controls.Clear();
TheScreen.Controls.Add(theObject);
This all works as expected also.
action3 button is the one that is giving me the problem and just like action2 it is handled on [main page] where it clears the PlaceHolder's controls and then dynamically creates and loads another UserControl which provide a simple form with one button on it.
UControl3 otherObject = (UControl3)Page.LoadControl("~/path/to/Control3.ascx");
TheScreen.Controls.Clear();
TheScreen.Controls.Add(otherObject);
The form is very simple, contains a <select> elements whose <option> are populated using an asp:Repeater based on an API call that provides the DataSource for it. It also has a simple <input> textbox field and lastly a submit <button>.
<select id="..." name="..." required="required">
<option disabled selected value="">select payment account</option>
<asp:Repeater ID="..." ClientIDMode="Static" runat="server" ItemType="model.namespace">
<ItemTemplate>
<option value="<%#: Eval("...") %>">
<%#: Eval("...") + " " + Eval("...") %>
</option>
</ItemTemplate>
</asp:Repeater>
</select>
<input id="..." name="..." type="text" required="required" runat="server" />
<button id="finish-action3" class="btn" runat="server" onserverclick="do-action3">Do It</button>
What I don't get is why finish-action3 is not performing the actions of the handler method do-action3, it simply refreshes back to the [main page] with the initial state that we started from with the initial Control1s being rendered and nothing else, no PostBack either.
I have searched extensively and found nothing addressing this specific scenario and what I have found, everyone keeps suggesting using asp:Button but that makes no difference for me in the behavior.
As written above, clicking finish-action3 button which is part of Control3 will not cause the basic HTML5 validation and does not trigger the do-action3 handler method and simply refreshes back to the [main page] where we started.
If I add do-action3 to the onsubmit attribute OR add type=submit, I get the validation but once it passes validation, the same behavior, nothing but load the [main page].
Changing the element to an asp:Button made absolutely no difference. The validation is triggered without anything special (like using onsubmit attribute or having a type=submit but once it is validated the same behavior, never calls do-action3 nor does it even trigger a Page_Load/PostBack on control3 just back to the [main page].
I suspect I am overlooking some event or is not bubbling and is getting lost in the process but I can't think what it is and where and why. Why doesn't the form that is last on the screen loaded by Control3 "submitted" by that button click not generating a PostBack or running the onserverclick handler method and simply refreshing to the main default state page, what am I missing here?
Any help would be appreciated as I have been banging my head against the wall trying to figure out what I am missing. I never encountered this in MVC or Core and it is making me crazy.
Let's simplify. Basically, what you are saying is that the following does not work because the btnFinishAction3_Click method of the dynamically-added Control3 user control is not firing:
Default.aspx (main page):
<asp:PlaceHolder runat="server" ID="plhPlaceHolder1"></asp:PlaceHolder>
<asp:Button runat="server" ID="btnAction3"
Text="Do Third Action"
OnClick="btnAction3_Click"/>
Default.aspx.cs:
protected void btnAction3_Click(object sender, EventArgs e)
{
AddControl3();
}
private void AddControl3()
{
Control3 objControl3 = (Control3)Page.LoadControl("~/Control3.ascx");
plhPlaceHolder1.Controls.Clear();
plhPlaceHolder1.Controls.Add(objControl3);
}
Control3.ascx:
<asp:Label runat="server" ID="lblMessage"></asp:Label>
<asp:Button runat="server" ID="btnFinishAction3"
Text="Finish Action 3"
OnClick="btnFinishAction3_Click"/>
Control3.ascx.cs:
protected void btnFinishAction3_Click(object sender, EventArgs e)
{
lblMessage.Text = "Finished Action 3.";
}
This is normal behavior. After postback the dynamically added user control does not exist and the event is "ignored". In ASP.NET Web Forms, every dynamic control must be re-added to the page after every postback. So, you need something like the following:
Default.aspx (main page):
<asp:PlaceHolder runat="server" ID="plhPlaceHolder1"></asp:PlaceHolder>
<asp:HiddenField runat="server" ID="hifControl3Loaded"/>
<asp:Button runat="server" ID="btnAction3"
Text="Do Third Action"
OnClick="btnAction3_Click"/>
Default.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (hifControl3Loaded.Value == "1")
{
AddControl3();
}
}
protected void btnAction3_Click(object sender, EventArgs e)
{
AddControl3();
hifControl3Loaded.Value = "1";
}
private void AddControl3()
{
Control3 objControl3 = (Control3)Page.LoadControl("~/Control3.ascx");
plhPlaceHolder1.Controls.Clear();
plhPlaceHolder1.Controls.Add(objControl3);
}
In other words, you need to add Control3 to the main page at every postback on Page_Load once the user has decided to add it by clicking the btnAction3 button in the first place.

How to check whether ASP.NET button is clicked or not on page load

How can I check whether a particular button was clicked or not in ASP.NET?
I think I need to perform some operation on Page_Load. This shouldn't be entering to Button_Click event to find. Is there any way that I can find where it was clicked or not on Client Side and take it to Page_Load?
Background: Basically __EVENTTARGET and __EVENTARGUMENT , These two Hidden controls are added to the HTML source, when ever any autopostback attribute is set to true for any of the web control.
The __EVENTTARGET hidden variable will tell the server ,which control actually does the server side event firing so that the framework can fire the server side event for that control.
The __ EVENTARGUMENT variable is used to provide additional event information if needed by the application, which can be accessed in the server.
So we can easily get the control causing postback using:Request.Params.Get("__EVENTTARGET");
PROBLEM:
The method: Request.Params.Get("__EVENTTARGET"); will work for CheckBoxes, DropDownLists, LinkButtons, etc.. but this does not work for Button controls such as Buttons and ImageButtons
The Button controls and ImageButton controls does not call the __doPostBack function. Because of this, the _EVENTTARGET will always be empty. However, other controls uses javascript function __doPostBack to trigger postback.
So, I will suggest to do something as below. Add an OnClientClick property to the buttons. Also, define a hiddenField in your Markup, whose value will contain the actual button causing postback.
<asp:Button ID="Button1" runat="server" Text="Button"
OnClientClick = "SetSource(this.id)" />
<asp:ImageButton ID="ImageButton1" runat="server"
OnClientClick = "SetSource(this.id)" />
<asp:HiddenField ID="hidSourceID" runat="server" />
On the OnClientClick property of the Button and ImageButton Call the SetSource JavaScript function
<script type = "text/javascript">
function SetSource(SourceID)
{
var hidSourceID =
document.getElementById("<%=hidSourceID.ClientID%>");
hidSourceID.value = SourceID;
}
</script>
Here onwards, you can very easily check in your Page_Load as to which Control caused postback:
if (IsPostBack)
{
string CtrlName;
CtrlName=hidSourceID.Value;
}
I just got the same trouble, have to do some logic judgement in the Page_Load method to treat different event(which button was clicked).
I realize the arm to get the as the following example.
The front end aspx source code(I have many Buttons with IDs F2, F3, F6, F12.
<Button Style="display: none" ID="F2" runat="server" Text="F2:Cancel" OnClientClick="SeiGyo(this)" OnClick="F2_Click" />
<Button Style="display: none" ID="F3" runat="server" Text="F3:Return" OnClientClick="SeiGyo(this)" OnClick="F3_Click" />
<Button Style="display: none" ID="F6" runat="server" Text="F6:Run" OnClientClick="SeiGyo(this)" OnClick="F6_Click" />
<Button Style="display: none" ID="F12" runat="server" Text="F12:Finish" OnClientClick="SeiGyo(this)" OnClick="F12_Click" />
The back end aspx.cs source code, what I need to do is judge which button was clicked when Page_Load was triggered. It seems a little stupid, but works.
In your situation, the button be clicked will be added into dic. I hope that will be helpful to some one.
Dictionary<string, string> dic = new Dictionary<string, string>();
foreach(var id in new string[]{"F2","F3","F6","F12"})
{
foreach (var key in Request.Params.AllKeys)
{
if (key != null && key.ToString().Contains(id))
dic.Add(id, Request[key.ToString()].ToString());
}
}
The UniqueID of the button will be in Request.Form["__EVENTTARGET"]
This question is already answered at: ASP.NET : Check for click event in page_load
You can try using the hidden field. Make the client side event on the OnclientClick event and try setting the value of hidden field, may be true or false depending on the condition.
And on the page load you can check the value of Hiidden field.
function click()
{
// set the hidden field here
}
And on the page load, simply check the value.
if(HiddenFieldName.Value=="true")
{
//perform the action
}
private bool button1WasClicked = false;
private void button1_Click(object sender, EventArgs e)
{
button1WasClicked = true;
}
if ( button1WasClicked== false)
{
//do somthing
}

Method being triggered on page load but not via asp button

I have two asp:Labels, the first of which is replaced with a few buttons and the second with a list of items.
I want to click on the buttons to filter the items.
The contents of the buttons are added programmatically by replacing the text with html and works fine.
asp:
<form id="form1" runat="server">
<asp:Label id="filters" runat="server" Text="Filters here"/>
<asp:Label id="itemList" runat="server" Text="List of items here"/>
</form>
resultant html of filters label:
<input type="submit" onclientclick="Load_Items(0)" runat="server" value="First"/>
<input type="submit" onclientclick="Load_Items(1)" runat="server" value="Second"/>
<input type="submit" onclientclick="Load_Items(2)" runat="server" value="Third"/>
relevant c#:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Load_Items(0);
}
}
public void Load_Items(int filterType)
{
//code to load items (pseudo below)
for each row in list
if filterType = itemType
build string
replace second label with string
}
On page load everything happens just as I want it to with the contents being filtered by the first item (hence Load_Items(0)), and if I manually change the 0 to another number in Page_Load, it filters by the other types, but if I click the buttons which are programmatically added, nothing happens other than what looks like the page refreshing.
I know the post back check is working by adding a text replacement before and inside it.
I've also added an asp:button to make sure it's not something to do with the way the buttons are added as below (with some extra things recommended from searches):
<asp:Button runat="server" CausesValidation="False" onclientclick="Load_Items(2); return false;" text="Submit" />
So what could be the issue?
The OnClientClick property specifies the javascript to run in the browser when the button is clicked. Since you probably don't have a javascript function called Load_Items, this will generate a script error, and the button will then cause the form to post back.
The server-side Click event will fire on the server, but doesn't allow you to pass a parameter. You will only get the button instance and an empty EventArgs instance.
You might be better off using the Command event, combined with the CommandArgument property.
<asp:Button runat="server" CommandArgument="2" OnCommand="Load_Items" ...
The event handler would use the CommandArgument property of the CommandEventArgs to access the argument from the clicked button:
protected void Load_Items(object sender, CommandEventArgs e)
{
Load_Items(Convert.ToInt32(e.CommandArgument));
}
Well, that's the common problem which I think every asp.net developer deals some time. The common part of it, that asp.net event system doesn't work, as windows forms.
Page object, and all controls on that page, have lifecycle events, that are triggered during any request, even when it's from update panel.
As you create those controls by code, you have to keep in mind, that all events for those controls should work as part of Page object. That's why you have to create those object in Page_Init event, before all other control's event would be triggered.
Please also keep in mind that you have to create those controls as asp.net objects:
var btn = new Button();
But not by simply adding html markup. And you have to recreate them on each request, following that one, when they were created.
Please take a look on my another answer.

ASP.NET How to read HTML Form Element?

I have an ASP.NET web form where I have an hidden field, like this:
<form id="form1" runat="server" action="http://localhost/fa/Default.aspx">
<div>
<input id="requestData" type="hidden" name="requestData" value="" runat="server" />
<asp:Button ID="btnPOST" Text="POST" runat="server" OnClick="do_POST" />
</div>
</form>
On the method do_POST I have this:
protected void do_POST(object sender, EventArgs e)
{
//requestDataField is of the type protected global::System.Web.UI.HtmlControls.HtmlInputHidden requestData;
requestDataField.Text = "FOO!";
}
When I submit the form (by pressing the button), it goes to the server (an handler) wheer I have this:
string requestData = context.Request.Form["requestData"];
I get an empty string..
But if I assign a value like this:
<input id="requestData" type="hidden" name="requestData" value="FOO" runat="server" />
I get the "FOO"
What am I missing?
The reason why it's not doing it is because the method is called after the page has been post back. Meaning, it is actually working if you change .Text to .Value unfortunately by that time you have already read your form and it was an empty value. I remember working on a project where you could tell your form not to submit until a function has been run (but it was with a javascript that needed to run an complete before aspx submitted). You should try to see if there is a way to force your form to run your function BEFORE doing the postback.
Your do_POST method runs on the server, not on the client, and so is setting the value of the server-side object which represents the <input> control. Your context.Request.Form["requestData"] gets the value of the field from the client side data submitted in the POST request, which was never set, so it is blank.
If you want the onClick to be a client-side function, then you need to do it a little differently. Use the OnClientClick attribute (instead of onClick). Then create a javascript method to set the field value:
<asp:Button ID="btnPOST" Text="POST" runat="server" OnClientClick="do_POST" />
<script>
function do_POST() {
document.getElementById("requestData").value = "FOO!";
}
</script>
I tried your code and did few changes to it.
Change requestDataField.Text = "FOO!"; to requestData.Value = "FOO";
Also I added two buttons. One for do_POST function and the UseSubmitBehaviour property is set as False. The other one was to submit the form.
If you want to set it on client side then you will have to use Javascript.
Use "Value" instead of "Text" property for HtmlInputHidden control:
requestDataField.Value = "FOO!";
instead of
requestDataField.Text = "FOO!";

Categories