ASP.NET viewstate update without javascript - c#

Hy,
how to update my viewstate on model variable value changed? To make it more clear lets take an example.
I have 3 variables in my model. One of them is bool and other two are strings. when I run my application I have a form of one checkbox. When I click on checkbox I want those two string values appear as a input values and when I click on the checkbox again I want to make them disappear again.
How could I make this happen without javascript?

when Using simple web form application just use
on aspx page
<asp:CheckBox Text="Check" Checked="false" ID="MyCheckBox" runat="server" AutoPostBack="true" OnCheckedChanged="MyCheckBox_CheckedChanged" />
on aspx.cs page
protected void MyCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (MyCheckBox.Checked)
{
Response.Write("CheckeD");
}
else
{
Response.Write("Un CheckeD");
}
}

Related

Set focus back to the proper textbox after autopostback function (Page.SetFocus doesn't solve the issue)

Now, this MAY look a duplicate, but it's not.
Every solution on the internet shows you how to get focus on the textbox that fired the event.
But what if the user presses tab? The textbox that should have focus is the next one.
So we do a workaround and focus on the textbox that have TabIndex higher than the one that fired the event.
But then what if the user presses Shift+tab?
Even worse: what if the user clicks on another random textbox?
This is the issue.
I don't think a code is required here, because it's a general solution to set focus on textboxes that have autopostback function.
If code is required, please ask in the comments.
The following will allow you to do what you want:
What we need to do is have js assist with what control will be next, in this case any control that is getting focus (whether it be via tab, shift-tab, click, or whatever control combination leaps out of the text box and onto a different control). By utilizing a WebMethod we can pass this information onto the server for AutoPostBack focus.
WebMethod:
[WebMethod]
public static void set_nextFocus(string id)
{
_toFocus = id;
}
Simple enough, _toFocus is class variable static string _toFocus, it holds the value of the next control to focus.
Page_Load
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
//sets focus to the proper control
Page.SetFocus(Page.FindControl(_toFocus));
}
}
JavaScript
in <head>
<script type="text/javascript">
function setFocus(x) {
PageMethods.set_nextFocus(x);
}
</script>
ASP controls
In this example, a TextBox. Note the use of OnFocusIn. It is an expando attribute of the ASP control which will realize there is no server-side definition, and revert to javascript's onfocusin attribute.
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" TabIndex="1"
ontextchanged="TextBox1_TextChanged" OnFocusIn="setFocus(this.id)" >
</asp:TextBox>
Also, in order to use PageMethods you must enable it within the form, like so:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />
You can check the __EVENTTARGET property of form which will tell you the textbox name from which the event has been raised.
Scenario, say I have two textboxes named TextBox1 and TextBox2 for both AutoPostBack set to true and hooked up textChanged event to single handler TextBox1_TextChanged. You can have below code and set the focus back to the specific textbox control
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
string target = Request.Form["__EVENTTARGET"];
if (target == "Textbox2") //conrol name should be exact
{
Page.SetFocus(this.TextBox2);
}
}

ASP.NET populate lists referencing each other on selected index change

I have an ASP.NET control with three listboxes: LBParent, LBChild1 and LBChild2.
LBParent has to be filled programmatically. I do this in Page_Load.
When the user selects a value in LBParent, I want to fill both LBChild1 and LBChild2 with some data, programmatically. The children lists must also keep track of their selected value.
Basically, the parent list is a list of users, the first list is a list of permissions the user has, the second list is a list of permissions the user doesn't have.
My plan is to add two buttons to move permissions between the two lists.
Unfortunately, I cannot get this to work properly.
If I populate the parent list in Page_Load, the selected index seems to reset. I used ViewState to save the index... but this seems to require an additional refresh, because it doesn't update after the PostBack. This is not acceptable.
If I populate the children listboxes on the OnParentSIC event, there is no way I can keep track of their selected index. The OnChildXSIC events never fire, because the listboxes get repopulated "too soon". (?)
How can I get this to work as intended? Maybe there is a better solution but I'd really like to understand how to get this solution to work, as I can't see a possible solution at the moment.
Control.ascx
<%# Control Language="C#" AutoEventWireup="true" EnableViewState="True" CodeBehind="..." Inherits="..." %>
<form runat="server">
<asp:ListBox ID="LBParent" runat="server" CssClass="form-control"
AutoPostBack="true" OnSelectedIndexChanged="OnParentSIC" />
<asp:ListBox ID="LBChild1" runat="server" CssClass="form-control"
AutoPostBack="true" OnSelectedIndexChanged="OnChild1SIC" />
<asp:ListBox ID="LBChild2" runat="server" CssClass="form-control"
AutoPostBack="true" OnSelectedIndexChanged="OnChild2SIC" />
</form>
Control.ascx.cs
protected void Page_Load(object sender, EventArgs e)
{
// Populate parent
for(...) LBParent.Items.Add(...);
}
The Onchange-event fires, but before that the OnLoad fires.
So:
The user clicks on an selection
A postback is triggered
At the server the Onload fires (and rebuilds the list)
The OnSelectedIndexChanged fires (which lost the selection by now)
So I would try to save the current-selected-index before you rebuild the list (in the Onload).
If you restore it later on while you are in the same 'postback' you can save it in a simple variable. No need to store in a Viewstate or Session.
use Like this
If You are on the Same Page then You can Use ViewState/Hidden Fields
to Maintain States
First Time Adding ListItem on PageLoad Use Like this
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
AddListItems();
}
}
Protected Void AddListItems()
{
// Populate parent
for(...) LBParent.Items.Add(...);
for(...) SecondList.Items.Add(...);
for(...) ThirdList.Items.Add(...);
}

How to pass a value from function in aspx to cs.file

I am sure that I am missing a small thing. I have ddl and a function, i want to pass the 'value attribute' of a chosen ddl option to my function in cs file.
But I cant get the value attribute..
-I checked the DDL and the Value attribute is fine and helds my info well.
--I tried using the word - this. to get my value but it didnt work..
aspx page
<asp:DropDownList ID="myDdl" runat="server" onchange='<%# orgenaize('here I need the value attribute') %>'/><br />
aspx.cs file
public void orgenaizeCheckBox(string currentId)
{
//do something
}
You can add javascript event handler using Attributes.Add in code behind where you can easily use the value from dll.
myDdl.Attributes.Add("onchange", "orgenaize(" + dllClass.Attribute + ");");
<asp: DropDownList ID ="ddlValue" runat ="server" AutoPostBack="True"
OnSelectedIndexChanged="ddlValue_SelectedIndexChanged"
ToolTip ="Please Select value">
</asp: DropDownList>
Then in your .cs class just handle the event
protected void ddlPhoneModel_SelectedIndexChanged(object sender, EventArgs e)
{
// What ever you want to do on selected index change
ddlValue.SelectedItem.Value
}
.Value should work, long time since i played around with Web controls but that should work

ASP.NET/C# trying to use page_load method on a form with #A on end of web address through link

I am needing to use the same form for displaying multiple things and I realize that when I add links like:
A
it has the same form and web address, but with a #A at the end of the address.I thought I could use this for displaying multiple things on the same form. My idea is to have C# code in the page_load method to detect what the web address is and use a conatins method for the url string and detect if there is #A to change the content of the form. Here is an example:
C# code:
protected void Page_Load(object sender, EventArgs e)
{
string url = HttpContext.Current.Request.Url.AbsoluteUri;
if(url.Contains("#A"))
{
div1.Visible = false; //content 1
div2.visible = true; //content 2
}
}
asp.net code:
A
<div ID="div1" runat="server">
content 1
</div>
<div ID="div2" runat="server">
content 2
</div>
I have tried to put the Page_Load method in a script tag, but still didn't work. I guess since the url is different the cs code is not valid? I know it goes through the page_load method once, before I click on the link. Also I do use a method that gives me the controls of div1 and div2, so that is not the problem. I thank everyone in advance for your help! Also if my way is not the way to do the job then please tell me any way possible to achieve what I am trying to do.
edit: I can't use a button to replace a link... maybe a asp:hyperlink?
That's an HTML hyperlink you're using and it won't cause a postback thus page_load will never get called when you click it.
I would suggest if you want to show an hide divs that you use client side JavaScript. Alternatively you could (for example) use an asp.net button control which will cause a postback.
I would suggest scrapping the anchor with an href approach in favor of this:
Use the ASP.NET server controls, along with their click event handlers to manage the visibility of controls on your page.
In your Page_Load, make it so the page has an initial state of showing controls, like this:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
div1.Visible = false; //content 1
div2.visible = true; //content 2
}
}
Now instead of an anchor tag, you can use an ASP.NET Button or LinkButton to cause a postback to the server, like this:
<asp:Button id="Button1"
Text="Click here to change page to B"
OnClick="Button1_Click"
runat="server"/>
Now you have the event handler code which would change the visibility of controls, like this:
protected void Button1_Click(Object sender, EventArgs e)
{
div1.Visible = true; //content 1
div2.visible = false; //content 2
}

dropdown event handler writing in asp.net and C#

What properties should I enabled in dropdown menu and how should I initiate the code in C#? I'm using Visual studio as my back end.
ASPX:
Set the auto postback and add an event handler:
<asp:DropDownList runat="server" id="foo" AutoPostback="true" OnSelectedIndexChanged="foo_SelectedIndexChanged>
</asp:DropDownList>
In the .CS
protected void foo_SelectedIndexChanged(Object sender, EventArgs e){
}
Hope this works for your requirements.
1. Getting dataset value from Database.
2. Biding for the required DropDownList
3. Selecting default value for the DropDownList.
Make Sure to check autopost back property to "True"
http://www.codeproject.com/Questions/400833/How-to-bind-or-populate-data-into-a-Dropdown-List

Categories

Resources