In my drop-down list, the SelectedIndexChanged event is not firing. I set AutoPostBack="True" but it's still not firing. Setting EnableViewState to True or False makes no difference either.
Here's my code:
<asp:DropDownList ID="ddlSheerName" runat="server" Width="250" AutoPostBack="True"
OnSelectedIndexChanged="ddlSheerName_SelectedIndexChanged"></asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
loggedInUserId = Convert.ToString(Session["LoggedInUserId"]);
if (loggedInUserId == "")
{
Response.Redirect("Login.aspx");
}
if (Page.IsPostBack == false)
{
BindCompanyDropDown();
}
}
protected void ddlSheerName_SelectedIndexChanged(object sender, EventArgs e)
{
Bindcolumnname();
}
public void BindCompanyDropDown()
{
try
{
objData = new DBFile();
DataSet dsCompanies = objData.GetCompaniesList(loggedInUserId);
if (dsCompanies != null)
{
if (dsCompanies.Tables[0].Rows.Count > 0)
{
ddlselectcompany.DataSource = dsCompanies;
ddlselectcompany.DataTextField = "CompanyName";
ddlselectcompany.DataValueField = "CompanyID";
ddlselectcompany.DataBind();
}
}
}
catch (Exception ex)
{
lblMsg.Text = ex.Message;
}
}
Viewstate must be enabled for this particular code to work and Javascript must be enabled for AutoPostBack to function.
The dropdown itself doesn't cause the event to fire.
You must actually change the selected item for the event to fire.
Is your event registred in the designer?
Select the dropdown and check the events assigned to it.
Related
I have a web-part in SharePoint 2013 which adds the new items from excel. The web-part contains upload control, buttons and textbox. I choose the document from upload control and click the button to load items in SP, if it was successfull I see "Successfull" in textbox or "Not successfull" in another way.
My problem: if i refresh page with web-part, textbox still contains the old text, but i want to see it empty after every refresh.
I try to use Page.IsPostBack, but I think I didn't properly use it.
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
textbox1.Text = "";
}
protected void btn3_Click(object sender, EventArgs e)
{
if (!Page.IsPostBack)
return;
if(!upload.HasFile)
{
textbox1.Text += "You didn't choose an Excel file";
return;
}
...
}
<asp:Button ID="btn3" runat="server" OnClick="btn3_Click" Text="Add Items" />
In such case, you can implement a special code block to detect browser refresh as
private bool refreshState;
private bool isRefresh;
protected override void LoadViewState(object savedState)
{
object[] AllStates = (object[])savedState;
base.LoadViewState(AllStates[0]);
refreshState = bool.Parse(AllStates[1].ToString());
if (Session["ISREFRESH"] != null && Session["ISREFRESH"] != "")
isRefresh = (refreshState == (bool)Session["ISREFRESH"]);
}
protected override object SaveViewState()
{
Session["ISREFRESH"] = refreshState;
object[] AllStates = new object[3];
AllStates[0] = base.SaveViewState();
AllStates[1] = !(refreshState);
return AllStates;
}
In the button click you can do it as
protected void btn3_Click(object sender, EventArgs e)
{
if (isRefresh == false)
{
Insert Code here
}
}
I tried this but it doesn't work. They're still greyed out even when I select stuff.
btnVoirFiche.Enabled = false;
btnEchangerJoueur.Enabled = false;
if (lstJoueurs.SelectedIndex != -1)
{
btnVoirFiche.Enabled = true;
btnEchangerJoueur.Enabled = true;
}
else
{
}
You'll want to handle the ListBox.SelectedIndexChanged event, and within your handler you're going to check if the specific value is the selected one, and then set you button's enable property accordingly.
Something like this:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(listBox1.SelectedIndex != -1)
{
btnVoirFiche.Enabled = true;
btnEchangerJoueur.Enabled = true;
}
else
{
//whatever you need to test for
}
}
Cheers
EDIT: I'm not too sure what your logic for button's enabled property is, so my answer is pretty generic. If you add details to you question, I'll adapt accordingly.
Hook into SelectedIndexChanged event and put your code inside of it
private void lstJoueurs_SelectedIndexChanged(object sender, EventArgs e)
{
if (lstJoueurs.SelectedIndex != -1)
{
btnVoirFiche.Enabled = true;
btnEchangerJoueur.Enabled = true;
}
}
As an alternative, and using mrlucmorin's answer, you could use the listbox's SelectedItem which will return null if nothing is selected.
i need to assign value for selected value drop down in aspx for example
dropdownlist items
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem>b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:DropDownList>
if user select any item in dropdownlist1 it should increment value 2 then
if user select any item in dropdownlist2 it should increment value 2
i need to display total
i tried this code
static int i = 0;
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
i += 2;
Label1.Text = "hello"+i;
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
i += 2;
Label1.Text = "hello"+i;
}
its working but problem is if user first select 1 in dropdown //i=2 then user select b //i=4 if user again select 1 //i=6. it should not increment if user select any value in particular drop down list. how to do it. any idea....
You're using a static variable so the i value will be kept between postbacks and will be common to all users, this is incorrect.
You need to store it in ViewState, HiddenField, or Session in order to keep the value between postbacks and also keep the value different for each user.
Here's what I would've done using ViewState:
private int Counter
{
get
{
if (ViewState["Counter"] == null)
{
return 0;
}
else
{
return (int)ViewState["Counter"];
}
}
set
{
ViewState["Counter"] = value;
}
}
private bool DropDown1Selected
{
get
{
if (ViewState["DropDown1Selected"] == null)
{
return false;
}
else
{
return (bool)ViewState["DropDown1Selected"];
}
}
set
{
ViewState["DropDown1Selected"] = value;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!this.DropDown1Selected)
{
this.DropDown1Selected = true;
this.Counter += 2;
}
Label1.Text = string.Format("hello{0}", this.Counter);
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
this.Counter += 2;
Label1.Text = string.Format("hello{0}", this.Counter);
}
Few of the answers above are talking about static variable getting reset after post back, this is incorrect, Static variables keep their values for the duration of the application domain. It will survive many browser sessions until you restart the web server Asp.net Static Variable Life time Across Refresh and PostBack
That being said, it is definitely not a good idea to use Static variables and instead go with the approaches suggested using Session or Viewstate.
About your question, I guess you want to increment the value only first time a value is chosen from the drop down list, to achieve this you would want to have a flag to let you know if the value is already selected, something on the below lines:
static bool DrpDown1;
static bool DrpDown2;
static int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DrpDown1 = false;
DrpDown2 = false;
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!DrpDown1)
{
i += 2;
Label1.Text = "hello" + i;
DrpDown1 = true;
}
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
if (!DrpDown2)
{
i += 2;
Label1.Text = "hello" + i;
DrpDown2 = true;
}
}
You need a temporary store like ViewState or Session to keep you values and get it back from
there.
private int GetValue()
{
return Int32.Parse(ViewState["temp"]);
}
private void SetValue(int i)
{
if(ViewState["temp"]==null)
{
ViewState["temp"]=i;
}
else
{
ViewState["temp"]= i+Int32.Parse(ViewState["temp"]);
}
}
and use it in your code as follows
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SetValue(2);
Label1.Text = string.Format("hello{0}", GetValue());
}
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
SetValue(2);
Label1.Text = string.Format("hello{0}", GetValue());
}
I am trying to set a value for a radcombobox based on the textchanged event(updatestatus) of one textbox.
Its not changing the value.
<telerik:RadComboBox ID="ddlStatus" runat="server" Enabled="false"/>
protected void updatestatus(object sender, System.EventArgs e)
{
if (txtname.Text != String.Empty)
{
if (ddlStatus.Text.Trim() == "Waiting")
{
ddlStatus.Text = "complete";
}
}
}
Can Some one suggest me if I am missing something.?
You can use FindItemByText to set value of Talerik dropdown:
RadComboBoxItem item = ddlStatus.FindItemByText("complete");
item.Selected = true;
Complete implementation in your case will be something like this:
protected void updatestatus(object sender, System.EventArgs e)
{
if (txtname.Text != String.Empty)
{
if (ddlStatus.Text.Trim() == "Waiting")
{
RadComboBoxItem item = ddlStatus.FindItemByText("complete");
item.Selected = true;
}
}
}
I am somewhat new to ASP.NET and I am confused by the syntax so I am a little lost. I am trying to hide/disable a button based on an if statement but I dont know how to disable or hide it. I have done C# before but this code looks unfamiliar to me.
Below is some of the code:
C# component:
protected override void Render(HtmlTextWriter writer)
{
string PostCtrl = Request.Params["__EVENTTARGET"];
if (PostCtrl == "AccColLocation_content$collisionLocation$EditColLocation")
{
valDropDownlist(((CustomControl.DropDownValidator)collisionLocation.FindControl("valLoc_municipality")), "CollisionLocation.municipality");
..............
}
}
HTML:
<ItemTemplate>
<asp:LinkButton ID="EditColLocation" runat="server" Text="Edit Collision Location" OnClick="CollisionLocation_Edit" />
</ItemTemplate>
valDropDownList Method:
protected void valDropDownlist(CustomControl.DropDownValidator valDropdown, string DataElement)
{
try
{
bool mvarRequired, srRequired;
DataTable dtDataElement = DBFunctions.DBFunctions.getDataElement(RepDateTime, DataElement);
string s = dtDataElement.Rows[0]["mvarRequired"].ToString();
mvarRequired = (dtDataElement.Rows[0]["mvarRequired"].ToString() == "True") ? true : false;
srRequired = (dtDataElement.Rows[0]["srRequired"].ToString() == "True") ? true : false;
valDropdown.HaveToSelect = (SelfReported) ? srRequired : mvarRequired;
}
catch (Exception err)
{
MessageBox("An error occurred while setting drop down validation rules. " + err.ToString());
}
}
All these buttons are in grid view.
I have something of this nature:
protected void deletedr(object sender, EventArgs e)
{
try
{
GridView gv = (GridView)FindControl("DriverInfo");
gv.DataSource = DBFunctions.DBFunctions.getInfo(ReportID.Value, "", 2); ;
gv.DataBind();
bool isSelectedLast = false;
DataTable dt = DBFunctions.DBFunctions.getInfo(ReportID.Value, "", 2);
if (dlDriverNo.SelectedValue == dt.Rows[dt.Rows.Count - 1]["DriverNo"].ToString())
{
isSelectedLast = true;
}
if (!(DBFunctions.DBFunctions.deleteDriver(ReportID.Value, dlDriverNo.SelectedValue, isSelectedLast)))
{
MessageBox(null);
}
else
{
dlDriverNo.Visible = false;
lblDelDriver.Visible = false;
delDriverconfim.Visible = false;
cancelDel.Visible = false;
dlDriverNo.Items.Clear();
gv.DataSource = DBFunctions.DBFunctions.getInfo(ReportID.Value, "", 2);
gv.DataBind();
}
}
catch (Exception err)
{
MessageBox("An error occurred while deleting the driver. " + err.ToString());
}
}
If your LinkButton is in a GridView, the most interesting problem is getting a handle on it. After you have a handle you can just set it to invisible or disabled:
linkButton.Visible = false;
or
linkButton.Enabled = false;
But to get a handle to the LinkButton control you will need to use .FindControl in a suitable event on the GridView control:
<asp:GridView ID="myGridView" runat="server" OnRowDataBound="myGridView_RowDataBound">
...
</aspGridView>
Then in the code behind you would have:
protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
var linkButton = (LinkButton)e.Row.FindControl("EditColLocation");
if (linkButton != null)
{
if (*your condition to check*)
linkButton.Visible = false;
}
}
Hope this will get you moving in the right direction.
you can try
valDropdown.Visible = false; //Mask the control
After the if condition write the below code.
Buttonname.visible=false;