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
}
Related
I want to implement "auto remember" feature of a form. This app is used by accountants who track cheque payments.
The accountant is entering a number of checks at a time and it will be time saving to maintain the project, beneficiary and project instead of choosing them every time you want to add a transaction.
The form looks like attached.
So far my attempts are failing and i keep seeing the dash entry '-' always at the top of each of the three dropdownfields as if i did nothing.
However 'watch' paramters shows that the Session values are properly retrieved and set.
I'm trying to save the previously entered values of those fields in a Session. Unless someone has another thought.
The nature of the fields in the asp file is as such
<asp:DropDownList ID="ddAccount" runat="server" Height="24px" OnSelectedIndexChanged="ddAccount_SelectedIndexChanged" >
<asp:ListItem>-</asp:ListItem>
</asp:DropDownList>
*<br />
<br />
<asp:DropDownList ID="ddProject" runat="server" Height="22px" >
<asp:ListItem>-</asp:ListItem>
</asp:DropDownList>
*<br />
<br />
<asp:DropDownList ID="ddBenificiary" runat="server" >
<asp:ListItem>-</asp:ListItem>
</asp:DropDownList>
*<br />
<br />
On Page_load I added the following Session getters and setters. I choose to populate first, i.e get the accounts, projects
etc then set the proper value based on the sessions.
protected void Page_Load(object sender, EventArgs e)
{
try
{
user = (User)Session["user"];
if (!IsPostBack)
{
if (Session["IsValidUser"] == null || Session["IsValidUser"].ToString() != "true")
Response.Redirect("Login.aspx", false);
/// Setting the Body tag.
Site1 m = (Site1)Master;
m.PageSection = "transactions";
//////////////////////////
Populate();
if (Session["BankAccount_ATx"] == null)
{
Session.Add("BankAccount_ATx", null);
}
else ddAccount.DataTextField = Session["BankAccount_ATx"].ToString();
if (Session["Project_ATx"] == null)
{
Session.Add("Project_ATx", null);
}
else ddProject.DataTextField = Session["Project_ATx"].ToString();
if (Session["BenefClient_ATx"] == null)
Session.Add("BenefClient_ATx", null);
else
ddBenificiary.DataTextField = Session["BenefClient_ATx"].ToString();
}
}
catch (Exception ex)
{
Response.Redirect("Login.aspx");
}
}
I save the chosen value in the session on the save button.
Session["BankAccount_ATx"] = ddAccount.SelectedValue;
Session["Project_ATx"] = ddProject.SelectedValue;
Session["BenefClient_ATx"] = ddBenificiary.SelectedValue;
Many thanks guys and gals
You want to set the selected value of the dropdownlist, like this:
if (Session["Project_ATx"] == null)
{
Session.Add("Project_ATx", null);
}
else {
// Find the list item in the drop down that mataches the value in your session
ListItem li = ddProject.Items.FindByValue(Session["Project_ATx"]);
//Check to see if the list item was found in the drop down
//If it's found, then make it the selected item
if ( li != null )
li.Selected = true;
}
If you were storing the Text value in Session then use this method instead:
FindByText
If you store your value using:
Me.Session.Item("LastDdlValue") = Me.YourDdlValue.SelectedValue
You can use this code in Page_Load event when Not Me.Page.IsPostBack:
If Not IsNothing(Me.Session.Item("LastDdlValue")) Then
Me.YourDdlValue.SelectedValue = Me.Session.Item("LastDdlValue")
End If
Please note that you need to populate your DropDownList before setting its .SelectedValue.
FindByText is the best method, alternatively you can iterate through the items in the dropdown using for loop compare each item with the value saved in session. If they are equal set selected index
for(int i=0;i<dropdownlist1.Items.count;i++)
{
if(dropdownlist1.Items[i].Text==Session["Project_ATx"].ToString())//use dropdownlist1.Items[i].Value if you have stored the dropdownlist item value instead of text
{
dropdownlist1.SelectedIndex=i;
break;
}
}
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
}
}
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 currently facing a problem. How to get the latest selected value from a asp.net checkbox list?
From looping through the Items of a checkbox list, I can get the highest selected index and its value, but it is not expected that the user will select the checkbox sequentially from lower to higher index. So, how to handle that?
Is there any event capturing system that will help me to identify the exact list item which generates the event?
If I understood it right, this is the code I'd use:
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
int lastSelectedIndex = 0;
string lastSelectedValue = string.Empty;
foreach (ListItem listitem in CheckBoxList1.Items)
{
if (listitem.Selected)
{
int thisIndex = CheckBoxList1.Items.IndexOf(listitem);
if (lastSelectedIndex < thisIndex)
{
lastSelectedIndex = thisIndex;
lastSelectedValue = listitem.Value;
}
}
}
}
Is there any event capturing system that will help me to identify the exact list item which generates the event?
You use the event CheckBoxList1_SelectedIndexChanged of the CheckBoxList. When a CheckBox of the list is clicked this event is called and then you can check whatever condition you want.
Edit:
The following code allows you to get the last checkbox index that the user selected. With this data you can get the last selected value by the user.
protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e)
{
string value = string.Empty;
string result = Request.Form["__EVENTTARGET"];
string[] checkedBox = result.Split('$'); ;
int index = int.Parse(checkedBox[checkedBox.Length - 1]);
if (CheckBoxList1.Items[index].Selected)
{
value = CheckBoxList1.Items[index].Value;
}
else
{
}
}
Below is the code which gives you the Latest selected CheckBoxList Item.
string result = Request.Form["__EVENTTARGET"];
string [] checkedBox = result.Split('$'); ;
int index = int.Parse(checkedBox[checkedBox.Length - 1]);
if (cbYears.Items[index].Selected)
{
//your logic
}
else
{
//your logic
}
Hope this helps.
Don't know about you, but as a user i wouldn't want the page to post back every time a checkbox item was checked.
This is the solution i would go with (jQuery):
Declare a server-side hidden field on your form:
<asp:HiddenField ID="HiddenField1" runat="server" EnableViewState="true" />
Then wire up client-side event handlers for the checkboxes to store checkbox clicked:
$('.someclassforyourcheckboxes').click(function() {
$('#HiddenField1').val($(this).attr('id'));
This is a lightweight mechanism for storing the ID of the "latest" checkbox clicked. And you won't have to set autopostback=true for the checkboxes and do an unecessary postback.
You dont HAVE to use jQuery - you can use regular Javascript, but, why do more work? =)
Then when you actually do the postback (on a submit button click i assume), just check the value of the hidden field.
Unless of course you WANT to postback on every checkbox click, but i can't envision a scenario in which you'd want this (maybe you're using UpdatePanel).
EDIT
The HTML of a checkbox list looks like this:
<input type="checkbox" name="vehicle" value="Bike" /> I have a bike
So, you can access three things:
Vehicle = $(this).attr('name');
Bike = $(this).attr('value');
I have a bike = $(this).html();
If you're trying to access the databound value, try the second technique.
Give that a try.
I'm having some trouble with my main menu on my ASP.NET website. What I want is, when a user clicks on a link, it should change it's color to black and remain black until the user clicks on the other links.
In other words: I want to highlight the selected link.
So far I've got it working using some workaround JavaScript (should consider using jQuery instead I think), but the problem is, that the style dosn't remain on page postback (or so it seems..)
My current javascript:
<script type="text/javascript">
var last = "none";
function LinkSelector(link) {
if (last != "none") {
document.getElementById(last).className = "NormalLink";
}
document.getElementById(link).className = "ActiveLink";
last = link;
}
</script>
As I wrote, it sure changes the class name when clicked, but dosn't remain that way when the new page is loaded.
Anyone got a workaround on this? :)
Thanks in advance.
All the best,
Bo
Typically, I'll use the ASP:Menu control. It provides a decent amount of control over the menu items in it.
For example, here's a generic method that I dump in a masterpage and run during the initial page hit. This method loops over the menu items in the menu control, and selects the menu item that corresponds to the current URL.
protected void Page_Load(object sender, EventArgs e) {
if (!Page.IsPostBack) {
SelectMenuItem();
}
}
private void SelectMenuItem() {
string rawurl = Request.RawUrl.ToLower();
rawurl = rawurl.Substring(rawurl.LastIndexOf("/") + 1);
if (rawurl.IndexOf("?") >= 0)
rawurl = rawurl.Substring(0, rawurl.IndexOf("?"));
foreach (MenuItem mi in mnuMain.Items) {
if (mi.ChildItems.Count == 0) {
if (mi.Value == rawurl) {
mi.Selected = true;
break;
}
}
else {
foreach (MenuItem cmi in mi.ChildItems) {
if (cmi.Value == rawurl) {
mi.Selected = true;
break;
}
}
if (mi.Selected)
break;
}
}
}
Here are a few of the menu items I have in my asp menu control. Note that I use the Value attribute to help me indicate which menu item pertains to the requested URL in the method above.
<Items>
<asp:MenuItem Text="Forms" Value="authorization">
<asp:MenuItem Text="New Authorization Form" Value="createauthform.aspx" NavigateUrl="~/CreateAuthForm.aspx"></asp:MenuItem>
<asp:MenuItem Text="Manage My Authorization Forms" Value="myrequests.aspx" NavigateUrl="~/MyRequests.aspx"></asp:MenuItem>
<asp:MenuItem Text="Audit Attendance Form" Value="auditform.aspx" NavigateUrl="~/AuditForm.aspx"></asp:MenuItem>
<asp:MenuItem Text="Tax Determination Statement" Value="taxstatement.aspx" NavigateUrl="~/TaxStatement.aspx"></asp:MenuItem>
</asp:MenuItem>
</Items>
Consider implementing your "last" variable as a static member of your function, here's how it's done. That way its value will be retained between function calls. I am not sure if it's retained between page loads, but you can test it.
If not, you'll have to somehow instruct ASP to remember the value. Regarding that, I'm as useful as a lump weight.