on my save button i want to clear all values of form i did the following for CheckBoxList beach.
But it doesn't work. Why so, it doesn't make values clear for checkbox list
Branch is filled like this:
protected void course_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
int courseId = Convert.ToInt32(course.SelectedValue);
DataTable dt;
dt = placementManager.GetBranchList(courseId);
if (dt.Rows.Count != 0)
{
Branch.DataSource = dt;
Branch.DataTextField = "branch_name";
Branch.DataValueField = "branch_id";
Branch.DataBind();
}
Btnsave.Visible = false;
GridView1.Visible = false;
}
catch (Exception ex)
{
COMMON.logger.Error("Error on course_SelectedIndexChanged:CompanySelected.aspx.cs", ex);
}
if (b)
{
gridNotExist.Text = "Records Successfully inserted for the displayed table, To insert new records select new entries";
this.ViewState.Remove("selectedList");
this.ViewState.Remove("dt");
Session.Abandon();
passout.SelectedIndex = 0;
company.SelectedIndex = 0;
txtpackage.Text = "";
course.SelectedIndex = 0;
Branch.DataSource = null;
Branch.DataBind();
vistDate.Text = "";
txtvenue.Text = "";
//GridView1.Visible = true;
}
}
aspx page has, of course, branch like this:
<asp:UpdatePanel id="update" runat="server">
<contenttemplate>
<td>
<asp:DropDownList ID="course" runat="server" AutoPostBack="True" OnSelectedIndexChanged="course_SelectedIndexChanged" />
<asp:CustomValidator ID="coursenecessaryForSaveButton" runat="server" ControlToValidate="course" ErrorMessage="Select A Course" OnServerValidate="coursenecessaryForSaveButton_ServerValidate" ValidationGroup="save" />
</td>
<td>
<asp:CustomValidator ID="courseNecessary" runat="server" ErrorMessage="Select A Course" OnServerValidate="courseNecessary_ServerValidate" ValidationGroup="verify" />
</td>
<td style="width: 101px">
Branch Name*
</td>
<td style="width: 126px">
<asp:CheckBoxList id="Branch" runat="server" />
</td>
</contenttemplate>
</asp:UpdatePanel>
<asp:CheckBoxList id="Branch" runat="server" >
is in a different UpdatePanel to the control which is triggering the asynchronous postback - and firing your eventhandler: course_SelectedIndexChanged.
Put the Checkbox List inside the UpdatePanel with id="update" and it will solve your problem.
Still, i would consider refactoring your HTML (and code while you're at it).
For instance - you have two UpdatePanel's - why?
The second one has nothing in it besides checkboxes. The purpose of an UpdatePanel is to dynamically update portions of a page (ie dynamic information) without a full postback.
Having nothing but static controls (checkboxes) inside an UpdatePanel isn't really serving any purpose.
However, my above answer is a stab in the dark at what you're after. You're code-behind code does'nt really make sense:
if (b == true)
Where is b defined??
Branch.DataSource = null
Branch.DataBind()
What are you binding??
Related
I have an user control that contains an update panel with a repeater. The repeater has a button used to delete records.
The problem is when I press the delete button, and a record from the repeater gets deleted, a postback on the whole page is triggered. From my understanding, only the section inside the update panel should refresh.
I should also mention that on my master page I have a script manager with the property "EnablePartialRendering" set to true.
Can someone help me with this problem? Thanks in advance.
ASP file:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="ActivityFiles.ascx.cs" Inherits="Training.User_Controls.ActivityFiles" %>
<asp:Repeater ID="FilesRepeater" runat="server">
<HeaderTemplate>
<table class="table table-bordered">
<tr>
<td><b>Name</b></td>
<td><b>Description</b></td>
<td><b>Actions</b></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<tr>
<td><%# Eval("Name")%></td>
<td><%# Eval("Description")%></td>
<td>
<asp:LinkButton ID="DownloadFile" runat="server" OnClick="DownloadFile_Click" Font-Size="Large"
file-path='<%# Eval("Url")%>'>
<span class="glyphicon glyphicon-floppy-disk text-info" aria-hidden="true"></span>
</asp:LinkButton>
<asp:LinkButton ID="DeleteFile" runat="server" OnClick="DeleteFile_Click" Font-Size="Large"
file-id='<%# Eval("Id") %>'
file-path='<%# Eval("Url") %>'>
<span class="glyphicon glyphicon-trash text-danger" aria-hidden="true"></span>
</asp:LinkButton>
</td>
</tr>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
.cs File:
protected void Page_Load(object sender, EventArgs e)
{
activityId = (int)Context.Items["activityId"];
if ( ! IsPostBack && Visible)
{
if (activityId != 0)
{
LoadFiles();
}
}
}
private void LoadFiles()
{
DataTable files = DatabaseHelper.SelectAsDatatable(FilesQueries.GET_ACTIVITY_FILES, new Dictionary<string, object> {
{ "activityId", activityId }
});
FilesRepeater.DataSource = files;
FilesRepeater.DataBind();
if (files.Rows.Count == 0)
{
FilesRepeater.Visible = false;
NoRecords.Visible = true;
}
}
protected void DeleteFile_Click(object sender, EventArgs e)
{
// TODO: modify this function to work only with the fild id
LinkButton deleteButton = (LinkButton)sender;
string fileId = deleteButton.Attributes["file-id"];
string filePath = deleteButton.Attributes["file-path"];
DatabaseHelper.ExecuteNonQuery(FilesQueries.DELETE_FILE, new Dictionary<string, Object>() {
{ "#fileId",fileId },
});
//delete file from disk
string path = Server.MapPath(filePath);
FileInfo file = new FileInfo(path);
if (file.Exists)
{
file.Delete();
}
LoadFiles();
}
Found the solution on another stackoverflow post (See Eugene S answer)
You have to manually add you control to Script Manager. You should do that inside File Repeater's Item created event. Good luck.
My dropdown list control always returns the first item on postback, I've tried every solution i've come across but to no avail.
Bascially, I have two data classes that are simply containers for data.
ConnectedRobots (representing connected robot with its controller's version, its IP address and some other properties), and MiseAJour (representing a single available update with its version and some other details describing it).
These two classes are then used to create objects representing each connected robot or update available. I'm then creating a List of update versions to use it as a data source for each Dropdown list.
as shown in the picture below :
Click to view
Here's the piece of code reflecting this view (Default.aspx) :
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplicationTest.WebForm1" Theme="Theme1" EnableEventValidation="false" EnableViewState="true"%>
...
<body>
<form runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
...
<div style="width:100%;">
<table class="table table-hover">
<thead>
<tr>
<th class="auto-style1">Référence Robot/Version Contrôleur</th>
<th class="auto-style1">Pays/Adresse IP</th>
<th class="auto-style1">Etat</th>
<th class="auto-style1">Versions des contrôleurs disponibles</th>
<th class="auto-style1">Planification</th>
<th>MAJ</th>
</tr>
</thead>
<tbody>
<asp:Repeater ID="repCRobots" runat="server" EnableViewState="true">
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblReferenceRobot" runat="server" Text='<%# Eval("ReferenceRobot") + " / " %> ' />
<asp:Label ID="lblVersionControleur" runat="server" Font-Bold="true" Text='<%# Eval("VersionControleur") %>'/>
</td>
<td>
<asp:Label ID="lblPays" runat="server" Text='<%# Eval("Pays") + " / " %> ' />
<asp:Label ID="lblIPRobot" runat="server" Font-Bold="true" Text='<%# Eval("IPRobot") %>'/>
</td>
<td>
<asp:Label ID="lblEtat" runat="server" Font-Bold="true" Text='<%# Eval("Etat") %>'/>
</td>
<td>
<asp:DropDownList ID="VersionsMAJs" runat="server" CssClass="bg-primary" EnableViewState="true" OnSelectedIndexChanged="VersionsMAJs_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
</td>
<td>
<span style="padding: 0px 10px 10px 10px">
<cc1:TimeSelector ID="TimeSelector1" runat="server" DisplayButtons="false" Font-Bold="true" BackColor="#cce6ff" BorderStyle="Dotted" CssClass="bg-info" BorderColor="White"></cc1:TimeSelector>
</span>
</td>
<td>
<asp:CheckBox ID='SelectMAJ' runat="server"/>
<asp:HiddenField ID="HiddenCheckBox" Value='<% #Eval("ReferenceRobot")%>' runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</tbody>
</table>
<asp:Button ID="MAJButton" runat="server" Text="Mettre à jour" CssClass="btn btn-success btn-lg btn-block"/>
</div>
And the code behind is the following :
public partial class WebForm1 : System.Web.UI.Page
{
protected List<MiseAJour> ListeMAJs;
protected List<string> ListeVersionsMAJs;
protected List<ConnectedRobots> ListeRobotsConnectes;
protected List<string> ListeRefRobots;
protected DropDownList ddlVersionMAJ;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetConnectedRobots();
Repeater repCRobots = this.FindControl("repCRobots") as Repeater;
repCRobots.DataSource = ListeRobotsConnectes;
repCRobots.DataBind();
foreach (RepeaterItem item in repCRobots.Items)
{
ddlVersionMAJ = item.FindControl("VersionsMAJs") as DropDownList;
ddlVersionMAJ.DataSource = ListeVersionsMAJs;
ddlVersionMAJ.DataBind();
}
}
}
private void GetConnectedRobots()
{
WebserviceRobots ws = new WebserviceRobots();
string[] delimiters = new string[] { "|", "||" };
string connectedRobots = ws.getConnectedRobots();
string majsDetails = ws.getMajsDetails();
string[] connectedRobotsInfos = connectedRobots.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
string[] majsInfos = majsDetails.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
ListeRobotsConnectes = new List<ConnectedRobots>();
ListeRefRobots = new List<string>();
ListeMAJs = new List<MiseAJour>();
ListeVersionsMAJs = new List<string>();
for (int i = 0; i < majsInfos.Length - 1; i += 3)
{
ListeMAJs.Add(new MiseAJour() { VersionMAJ = majsInfos[i], DetailsMAJ = majsInfos[i + 1], Commentaires = majsInfos[i + 2] });
}
ListeVersionsMAJs = ListeMAJs.Select(v => v.VersionMAJ).ToList();
for (int i = 0; i < connectedRobotsInfos.Length - 1; i += 5)
{
ListeRobotsConnectes.Add(new ConnectedRobots() { ReferenceRobot = connectedRobotsInfos[i], VersionControleur = connectedRobotsInfos[i + 1], IPRobot = connectedRobotsInfos[i + 2], Pays = connectedRobotsInfos[i + 3], Etat = connectedRobotsInfos[i + 4], VersionsMAJ = ListeVersionsMAJs });
}
ListeRefRobots = ListeRobotsConnectes.Select(r => r.ReferenceRobot).ToList();
}
protected void VersionsMAJs_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList dll = (DropDownList)sender;
string test = dll.SelectedValue;
}
the GetConnectedRobots() method is used to retrieve both update versions, and connected robots using a web service and couple of dynamically generated xml files.
On selectedIndexChanged, I should get the selected value or so I thought. SelectedValue always returns the first element of the dropdown list. What am I doing wrong?
I do have EnableViewState="true" for persistence between postbacks.
I am open to all kinds of answers, even if it means restructuring the whole code,
Thank you for your answers,
So I resolved the issue after almost going insane, the problem weirdly enough was with getMajDetails() implemented in the WebserviceRobots. This method is used to deserialise an XML document containing updates versions and some other informations, and to return a string with these elements seprated by | character. However, it seems like XDocument automatically adds new line char at the end of each element, resulting in a string similar to this one :
1.7.1940\r\n| Détails concernant le MAJ 1.7\r\n| Commentaires 1.7\r\n||2.0.2542\r\n| Détails concernant le MAJ 2.0\r\n| Commentaires 2.0\r\n||1.8.2495\r\n| Détails concernant le MAJ 1.8\r\n| Commentaires 1.8\r\n||
So, each ListeVersionsMAJs element ends up with \r\n at the end. and When this list is used to populate the dorpdown list, it causses it to behave the way it did (returning the first value at each postback). the solution was to remove \r\n .
I have no explanation as to why this is happening, testing the dropdown list with a List of strings initialized with fixed string values with new line char at the end of each value resulted in the same odd behavior.
So if anyone could clear this up, it would be much appreciate it.
VS2013, WebForms, .NET 4.51
I have a FormView defined and in my EditTemplate I have as follows:
<div class="form-group">
<asp:Label ID="lblClientClassification" CssClass="col-md-2 control-label" runat="server" for="cblClientClassifications" Text="Kind"></asp:Label>
<div class="col-md-5">
<asp:CheckBoxList ID="cblClientClassifications" runat="server"></asp:CheckBoxList>
<asp:CustomValidator ID="cfvClientKinds" runat="server" Display="Dynamic" CssClass="label label-danger" ErrorMessage="XXXX" ValidationGroup="Default" OnServerValidate="cfvClientClassifications_OnServerValidate"></asp:CustomValidator>
</div>
and then in the code behind:
protected void cfvClientClassifications_OnServerValidate(object aSource, ServerValidateEventArgs aArgs)
{
CustomValidator cvCheckBoxKinds = aSource as CustomValidator;
int checkedCount = 0;
if (cvCheckBoxKinds != null)
{
CheckBoxList cblClientClassifications = GuiClientClassificationsFind();
foreach (ListItem listItem in cblClientClassifications.Items)
{
if (listItem.Selected)
{
checkedCount++;
}
}
if (checkedCount == 0)
{
aArgs.IsValid = false;
cvCheckBoxKinds.ErrorMessage = "Select client kind.";
}
}
}
The OnServerValidate is firing and I am getting to set the validator to invalid as well as setting the error message (Page.IsValid is also false as expected). However, the error text is not displaying. When I view the page source I see:
<span id="ctl00_cphMainContent_fvData_cfvClientKinds" class="label label-danger" style="display:none;">XXXX</span>
instead of the error message I set as well as the fact that it is not visible.
Has anyone got any pointers here on how to track this down? I have looked at similar questions of SO but none of the comments seem to apply. Is this related to FormView perhaps?
Try your control without the CssClass="label label-danger" bootstrap first, and use the code below to check your boxes:
protected void cfvClientKinds_ServerValidate(object aSource, ServerValidateEventArgs aArgs)
{
aArgs.IsValid = cblClientClassifications.SelectedItem != null;
cfvClientKinds.ErrorMessage = "Hey! this is a new message";
}
and I guess you call this line before you fire the above event:
protected void btnValidate_Click(object sender, EventArgs e)
{
Page.Validate();
}
In Short, I think that your problem is either related to your way of finding cblClientClassifications checkBoxList or other code that you haven't stated above.
CheckBoxList cblClientClassifications = GuiClientClassificationsFind();
I decided to try out your case and created a new webform added formview and bind it to northwind categories table then inside edititemtemplate I added a checkboxlist and populated it manually. added CustomValidator double clicked it copied your codebehind and it works for me except for the findcontrol part: GuiClientClassificationsFind();
Here is the formview:
<asp:FormView ID="FormView1" runat="server" DataKeyNames="CategoryID" DataSourceID="SqlDataSource1">
<EditItemTemplate>
...
<asp:CheckBoxList ID="cblClientClassifications" runat="server">
<asp:ListItem>Bir</asp:ListItem>
<asp:ListItem>iki</asp:ListItem>
<asp:ListItem>Üç</asp:ListItem>
<asp:ListItem>Dört</asp:ListItem>
</asp:CheckBoxList>
<asp:CustomValidator ID="cfvClientKinds" runat="server" Display="Dynamic" CssClass="label label-danger" ErrorMessage="CustomValidator" OnServerValidate="cfvClientKinds_ServerValidate"></asp:CustomValidator>
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
</asp:FormView>
And codebehind with your code:
protected void cfvClientKinds_ServerValidate(object aSource, ServerValidateEventArgs aArgs)
{
CustomValidator cvCheckBoxKinds = aSource as CustomValidator;
CheckBoxList cblClientClassifications = (CheckBoxList)FormView1.FindControl("cblClientClassifications");
int checkedCount = 0;
if (cvCheckBoxKinds != null)
{
foreach (ListItem listItem in cblClientClassifications.Items)
{
if (listItem.Selected)
{
checkedCount++;
}
}
if (checkedCount == 0)
{
aArgs.IsValid = false;
cvCheckBoxKinds.ErrorMessage = "Select client kind.";
}
}
}
I Ali Shahrokhi's method is shorter and works as well as yours..
protected void cfvClientKinds_ServerValidate(object aSource, ServerValidateEventArgs aArgs)
{
CustomValidator cvCheckBoxKinds = aSource as CustomValidator;
CheckBoxList cblClientClassifications = (CheckBoxList)FormView1.FindControl("cblClientClassifications");
aArgs.IsValid = cblClientClassifications.SelectedItem != null;
cvCheckBoxKinds.ErrorMessage = "Select client kind.";
}
if you check as you entered edititemtemplate in formview you'll see before submitting that your customvalidators span will be there just as you mentioned that's because server hasn't sent it to the client yet somehow.
I am new to ASP.NET. I am having problem with NavigateUrl.
<td align="right" valign="middle">
<p id="posCstmr">
<asp:HyperLink ID="hlnkContact" CssClass="addbtn-cmplist" runat="server" NavigateUrl='<%# "Actions/Contact.aspx?ContactID=" + Eval("ContactID") + "&CompanyID=" + Eval("CompanyID") %>' Text="View"></asp:HyperLink>
</p>
</td>
When I click to view following hlnkContact, it redirects to the following link with specified values which is OK.
[http://localhost:1426/Actions/Contact.aspx?ContactID=78724&CompanyID=92971]
But I want to store these values in session variables on Page_load event of Contact.aspx.
if (!Page.IsPostBack)
{
Session["ContactID"] = String.IsNullOrEmpty(Request.QueryString["ContactID"].ToString()) ? String.Empty : Session["ContactID"];
}
But I cant store the Session variable on Page_Load because NavigateUrl show the values after loading of this page.
Please Help me.
Same type of question was answered HERE, you can tweak like below:
[Assuming that the td is inside any databound container, else Eval wouldn't work]
Change your markup to this:
<td align="right" valign="middle">
<p id="posCstmr">
<asp:LinkButton ID="LinkButton1" CssClass="addbtn-cmplist" runat="server"
Text="View" CommandName="Link" CommandArgument='<%#Eval("ContactID") + ";" + Eval("CompanyID") %>'
OnClick="ButtonLink_Click" />
</p>
</td>
Code:
protected void ButtonLink_Click(object sender, System.EventArgs e)
{
LinkButton lb = (LinkButton)sender;
string[] arguments = lb.CommandArgument.Split(';');
string ContactID = arguments[0];
string CompanyID = arguments[1];
//Save in session
Session["ContactID"] = ContactID ;
Session["CompanyID"] = CompanyID ;
//Redirect
Response.Redirect(string.Format("Actions/Contact.aspx?ContactID={0}&CompanyID={1}", ContactID, CompanyID));
}
I have added an extra step to my uzerwizard
<asp:TemplatedWizardStep id="stpPayment" runat="server" Title="Payment">
<ContentTemplate>
<asp:DropDownList ID="cmbSubs" runat="server" ClientIDMode="Static"
Width="200px">
<asp:ListItem Value="month">Monthly Subscription</asp:ListItem>
<asp:ListItem Value="year">Annual Subscription</asp:ListItem>
</asp:DropDownList>
i am successfully navigating to the new step
protected void NewUserprofileWizard_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
if (NewUserprofileWizard.ActiveStepIndex == 0)
{
NewUserprofileWizard.ActiveStepIndex = 1;
}
}
but I cant get access to the dropdownlist from my codebehind
note: i can get a handle onto the controls in the 1st (createuser) step.
but any controls in the next step always return a null.
this is the code i am using
DropDownList cmb = (DropDownList)NewUserprofileWizard.WizardSteps[1].FindControl("cmbSubs");
i always return a null.
note that this works just fine
TextBox tmp = (TextBox)NewUserprofileWizard.CreateUserStep.ContentTemplateContainer.FindControl("Email");
userProfile.AccountEmail = tmp.Text;
problem seems unique to custom steps
Thanks for the help
Tried Gregors suggestion. no luck. mine always comes up as null.
if thsi helps any:
my wizard is inside a user control..
the page that uses the user control is inside a master page.....
Here is little sample I have created for you, first aspx code:
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server" OnNextButtonClick="CreateUserWizard1_NextButtonClick">
<WizardSteps>
<asp:WizardStep runat="server" Title="My Custom Step">
<asp:DropDownList ID="cmbSubs" runat="server" ClientIDMode="Static"
Width="200px">
<asp:ListItem Value="month">Monthly Subscription</asp:ListItem>
<asp:ListItem Value="year">Annual Subscription</asp:ListItem>
</asp:DropDownList>
</asp:WizardStep>
<asp:CreateUserWizardStep runat="server" />
<asp:CompleteWizardStep runat="server" />
</WizardSteps>
</asp:CreateUserWizard>
And now the code to find first dropdown:
protected void CreateUserWizard1_NextButtonClick(object sender, WizardNavigationEventArgs e)
{
if (e.CurrentStepIndex == 0)
{
//get instance to dropdown...
string selectedValue = null;
string controlId = null;
foreach (var item in CreateUserWizard1.WizardSteps[0].Controls)
{
DropDownList ddl = item as DropDownList;
if (ddl != null)
{
selectedValue = ddl.SelectedValue;
controlId = ddl.ClientID;
break;
}
}
}
}
Of course you can also find your dropdown like this:
DropDownList cmbSubs = CreateUserWizard1.WizardSteps[0].FindControl("cmbSubs") as DropDownList;
Happy coding!
it appears that today my google foo was doing much better
because i am in a templateswizardstep, i have to cast the wizardstep into the templatedwizardstep.
from here i can now find the control. whooohoo!
TemplatedWizardStep step = (TemplatedWizardStep)NewUserprofileWizard.WizardSteps[1];
cmb = (DropDownList)step.ContentTemplateContainer.FindControl("cmbSubs");
Thanks all for the help