radiobutton checked validation throws an error - c#

I'm having some trouble with radio button validation in a wpf application, the radiobutton.checked function it throws an error. the 2 radio buttons are name TSBtn and DSBtn. the error that I get when I try and build the application is'
Error 2 The event 'System.Windows.Controls.Primitives.ToggleButton.Checked' can only appear on the left hand side of += or -= 115 23 MainServerWIndow
im not sure why this is happening the code I have Writen is bellow'
private void Bill_Click(object sender, RoutedEventArgs e)
{
if (TSBtn.Checked)
{
StringBuilder itm = new StringBuilder();
foreach (object selectedItem in yourOrder.Items)
{
itm.AppendLine(selectedItem.ToString());
}
MessageBox.Show("The server is " + serversname.SelectedItem + "\n" + "The table number is " + int.Parse(TableNumber.Text) + "\n" + itm);
}
else if (DSBtn.Checked)
{
yourOrder.Items.Add("The driver is " + DriverName.SelectedItem);
yourOrder.Items.Add("Delivery Address " + DeliveryAddress.Text);
}
}
does anyone have any ideas as to why this is happening?
thanks.

Your answer lies in the error message.
The event 'System.Windows.Controls.Primitives.ToggleButton.Checked' ...
Checked is an event, not a property.
What you want is the IsChecked property.

Checked is an Event. You should be testing the checked property which is IsChecked.
So your code should read:
if (TSBtn.IsChecked)
...
and
else if (DSBtn.IsChecked)
...

I've found this question solved at:
stackoverflow.com
if (radLot.IsChecked == true)
{
SymbolSpecification = "LotRenderer";
}

Related

Use multiple fields for mailto: link in gridview

I'm trying to build a gridview field that displays a name and links to an email address with a subject. I first tried to make this work using DataNavigateUrlFormatString, but apparently using a : in a DataNavigateUrlFormatString breaks the link, so unfortunately the code below leaves me with an empty link.
string[] UrlFields = { "bidd_name", "Email", "bidd_number", "bidd_desc", "Bidds_id" };
LinkField = new HyperLinkField();
LinkField.HeaderText = "Buyer";
LinkField.DataTextField = "buyer";
LinkField.HeaderStyle.CssClass = "GVpadding";
LinkField.ItemStyle.CssClass = "GVpadding";
LinkField.DataNavigateUrlFields = UrlFields;
LinkField.DataNavigateUrlFormatString = "mailto:{1} ?subject={2} {3}";
Next I tried using Eval() and NavigateUrl, but the code below gives me this error "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
EmailLinkField.NavigateUrl = "mailto:" + (string)Eval("Email") + "?subject=" + (string)Eval("bidd_number") + " " + (string)Eval("bidd_desc");
I did a little research on the error and found out that I might have to use a RowDataBound method, but I'm not sure how to go about this. Could calling the method below after building out the field change the URL to the desired value?
protected void grddata_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hyp = (HyperLink)e.Row.FindControl("EmailLinkField");
hyp.NavigateUrl = "mailto:" + (string)Eval("Email") + "?subject=" + (string)Eval("bidd_number") + " " + (string)Eval("bidd_desc");
}
}
If so, how do I go about calling it? I'm not sure what parameters I need to use with it when doing something like this.
grddata_RowDataBound(null, null);
Using the nulls generates a "Object reference not set to an instance of an object." error and I don't know what value the sender or e should be.
If anybody could help me get these emails links generated it would be greatly appreciated. I have very little experience with C#, and haven't done all that much coding in general, so feel free to treat me like a child.
simply use a templated column will fix your issue
https://msdn.microsoft.com/en-us/library/bb288032.aspx

ASP.net web page update issue ( Postback?)

Greeting of the day everyone.
Hoping you are all well. I need your help to solve a simple issue ( I an new to C# + ASP.net).
I have created a ASp.net portal. When a user open that portal (web page). the Webpage give him a list of Groups whose he is the member of and the list of group whose he can add to himself..
It also contain two Drop Downs. One contains the list of groups, whose use can add to themselves. The second one contains the list of groups , whose the user is member of, and he can remove it from those groups.
Adding and removing code is working fine. But I have to refresh the page manually to show the updated information. After clicking the Add or Remove Button page is not refreshing with the updated information.
I tried to use redirected to main page using Response.Redirect("Success.aspx")
But nothing working,
protected void Page_Load(object sender, EventArgs e)
{
string uN = "domain\\michael.jackson";
//SystemResources sr = new SystemResources();
//ActiveUser usr = sr.GetUserDetails(uN).FirstOrDefault();
LabelUserName.Text = "michael.jackson";
// Getting the list of groups using of praticular OU the classOrgUnitGroup
string sdomain = ClassGroupPortalSettings.getDomainName();
string sInterstOU = "OU=testing,DC=analysys,DC=com";
classOrgUnitGroup a = new classOrgUnitGroup();
List<string> allGroups = a.GetGroupFromOu(sdomain, sInterstOU);
string grouplist = "<ul>";
foreach (string group in allGroups)
{
grouplist = grouplist + "<li><a href='showgroupmembers.aspx?group=" + group + "'>" + group + "</a></li>";
}
grouplist = grouplist + "</ul>";
lblOpenGroups.Text = grouplist;
//// 4. Finding users group membership.
string sDomain = ClassGroupPortalSettings.getDomainName();
classUserGroupMembership myMembership = new classUserGroupMembership();
List<string> myGroupsMem = myMembership.getMyGroupMembership(sDomain, "domain\\michael.jackson");
string glList = "<ul>";
string openlList = "<ul>";
string otherGroup = "<ul>";
foreach (string grp in myGroupsMem)
{
//BulletedListMyGroups.Items.Add(grp);
glList = glList + "<li>" + grp + "</li>";
if (allGroups.Contains(grp))
{
DropDownListMyGroups.Items.Add(grp);
openlList = openlList + "<li>" + grp + "</li>";
}
else
{
otherGroup = otherGroup + "<li>" + grp + "</li>";
}
}
glList = glList + "</ul>";
openlList = openlList + "</ul>";
otherGroup = otherGroup + "</ul>";
LabelOtherGrp.Text = otherGroup;
LabelOpenGrp.Text = openlList;
// LabelMyGroup.Text = glList;
foreach (string emailGroup in allGroups)
{
if (!myGroupsMem.Contains(emailGroup))
{
DropDownListOpenGroups.Items.Add(emailGroup);
}
}
}
This is code which run when a page reload.
The issue is : When I click on Remove Button or Add Button, it run the code to add or remove user from selected group. when page reload after clicking on button. The Group membership labels. and Dropdown box is not updating with code :(
I'm assuming your scenario like this, you're having two Drop down. One is full of records and the items can be select. Once items are selected, user will submit the button then records will save into database/xml. Later you're retrieving the items from db/xml and showing that in second Drop down.
Now you're issue is that the records are not showing in second Drop down after clicking the submit button,
If it so, you have two options.
1) You can pull your records as soon as insert things made into db.
2) You can pull your records in Page load itself as I mentioned below,
If(Postback) // Or If(PostBack == true)
{
// Pull your records and display in Drop down.
}
It is just the order of events which causing confusion IMO. Page_Load is called first and then control events(click, change etc). So on add/delete you need to rebind the items.
https://msdn.microsoft.com/en-gb/library/ms178472%28v=vs.100%29.aspx
Move out the data bind logic to a function and call function at the end of click event as well. Then you can reuse the function in page load and put logic to bind in page load only of its not postback because you might be rebinding unnecessarily in page load.
protected void Page_Load(object sender, EventArgs args)
{
if(!IsPostBack)
{
DataBindLogic();
}
}
private void DataBindLogic()
{
/*Put databind code here */
}
protected void RemoveBtn_Click(Object sender,Eventargs args)
{
/*Do db update */
DataBindLogic();
}
Thanks a lot everyone and and each of you for your time and help and energy, thanks a ton.
The issue is resolved, I have added another lines of code in the Postback, which checks the action of the User.
For example, if used Remove from the Group,
Then get the name of the Group and use action REMOVE
Then Update the DropDown box and Current membership box.
I did the same for the add group too, and it resolved the issue for me.
Thanks again , and whishing you a great day ahead.
Regards and Best wishes
Aman

Object not a function error

I am trying to call a javascript modal that says 'no units selected' if the user clicks a 'remove' button without clicking at least one checkbox next to a unit on a webpage. If no units are present, and the user clicks the 'remove' button, the script successfully executes and the 'not units selected' modal appears. However, if units are present, an error is thrown and occurs at the sixth line of the script (if (document.units('Select...etc) ) ). The error states the object (in this case, units) is not a function.
The script:
<SCRIPT>
function CheckTheForm()
{
var found = false;
var PersonsIDs = "";
for (i=1;i<=document.units.add_counter.value;i++)
if (document.units('SelectUnit' + i).checked == true)
{
if (PersonsIDs != "")
PersonsIDs += ",";
= PersonsIDs += document.units('SelectUnit' + i).value;
document.units.action = "UnitsRemove.asp?ID=" + PersonsIDs + "&RA=Units.asp";
found = true;
}
if (found==false)
{
alert("No units have been selected.");
return false;
}
//alert(document.units.action);
}
</SCRIPT>
The form that calls the script:
The button pressed to remove the units:
Any help would be greatly appreciated!
I'm guessing you're trying to do this
document.units['SelectUnit' + i].checked
which is how you'd access an objects properties with a dynamic key

ASP.NET - How to display javascript alert using C#?

I have a page that contains a textbox and button, when the user clicks the submit button I want to display a message based on a bool value.
I have researched on stackoverflow and tried the code in this questions: Asp.net Webform Display Alert and redirect
But it didn't work. I have the following code:
ClientScript.RegisterStartupScript(this.GetType(),"", "alert('message')", true);
What code do I need to display the alert message?
you can use this simple method:
private void MessageBox(string msg)
{
Label lbl = new Label();
lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
Page.Controls.Add(lbl);
}
Use this apporch
Response.Write(#"<script language='javascript'>alert('The following errors have occurred: \n" + strErrorDesc + " .');</script>");

Postback not maintaining selected values

I have a page with several dynamically generated DropDownLists. The DDs load and display the correct values on page load. However, when I try to retrieve the values at post back, the DDs all seem to be maintaining the values they had at page load.
All are created in Page_Load;
No test for IsPostBack;
Processing is handled in code below:
void btnSubmit_Click(object sender, EventArgs e)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath("\\") + "\\Logs\\Permissions.log",false);
string szMask = hMask.Value.ToString();
sw.WriteLine("\t\t\t\t\t\t\t" + szMask);
foreach (Control c in Page.Controls)
LoopControls(c, szMask, sw);
sw.Close();
}
private void LoopControls(Control Page, string szMask, System.IO.StreamWriter sw)
{
foreach (Control c in Page.Controls)
{
if (c is DropDownList)
{
string szId = c.ID;
if (szId.StartsWith("ddlPerm"))
{
string[] szSplit = szId.Split(':');
int iMaskPosition = Convert.ToInt32(szSplit[1].ToString());
int iSecurityPermissionID = Convert.ToInt32(szSplit[2].ToString());
DropDownList dd = (DropDownList)c;
string szPermission = dd.SelectedValue.ToString();
if (szMask.Substring(iMaskPosition, 1) != szPermission)
{
sw.WriteLine("NE");
if (iMaskPosition == 0)
szMask = szPermission + szMask.Substring(1);
else
szMask = szMask.Substring(0, iMaskPosition) + szPermission + szMask.Substring(iMaskPosition);
}
sw.WriteLine(szId + "\t\t" + iMaskPosition.ToString() + "\t" + iSecurityPermissionID.ToString() + "\t" + szPermission + "\t\t" + szMask);
}
}
else
{
if (c.Controls.Count > 0)
{
LoopControls(c, szMask, sw);
}
}
}
}
This is really bugging me. Any help would be greatly appreciated.
Thanks,
jb
Normally, this problem can be solved by
if (!IsPostback){
// bind all your dropdownlist here
}
Otherwise the selected values will be lost after rebinding.
ViewState is maintained between the Init and Load events. By creating and populating your controls during Load, you're basically coming in after ViewState has already been handled. Create your controls during Init and you should notice your postback values sticking.
For more information regarding what happens between and at each particular stage of the life cycle, consult the information at this link: http://msdn.microsoft.com/en-us/library/ms178472.aspx
The problem may be, as you said No test for IsPostBack. You're likely overwriting the values and state each time.
Instead, test for IsPostback and don't write them out if it's true.
All are created in Page_Load; No test
for IsPostBack; Processing is handled
in code
If you aren't testing for IsPostBack then on every page load the dropdowns are being recreated.
You need to test for IsPostBack and only create the dropdowns when it's not a PostBack. AKA - On the first load.

Categories

Resources