I've got 2 linkbuttons, they are linked to a multiview and depending on which I push will change the active view. I want the linkbutton of it's respective view to appear as it is active state.
<asp:Panel runat="server" >
<div>
<asp:LinkButton ID="linkDeviceList" CommandName="SwitchViewByID" CommandArgument="viewDeviceList" runat="server" OnClick="linkDeviceList_Click" CssClass="button-link">Device List</asp:LinkButton>
<asp:LinkButton ID="linkFTPFolders" CommandName="SwitchViewByID" CommandArgument="viewFTPFolders" runat="server" OnClick="linkFTPFolders_Click" CssClass="button-link">FTP Folders</asp:LinkButton>
</div>
</asp:Panel>
The event handlers. I assumed I'd change the button's state in a 'while' but can't figure out how to apply the style change.
protected void linkFTPFolders_Click(object sender, EventArgs e)
{
MultiView1.SetActiveView(viewFTPFolders);
while (MultiView1.GetActiveView() == viewFTPFolders)
{
}
}
protected void linkDeviceList_Click(object sender, EventArgs e)
{
MultiView1.SetActiveView(viewDeviceList);
while (MultiView1.GetActiveView() == viewDeviceList)
{
}
}
I have a similar control. And this is what I did.
I removed/added the "active" class on the button that was clicked.
I disabled the button that was clicked so that it can't be clicked
again.
protected void lbListView_Click(object sender, EventArgs e)
{
lbGridView.CssClass = "btn btn-default btn-sm pull-right dt-margin-left-5";
lbGridView.Enabled = true;
lbListView.CssClass = "btn btn-default btn-sm pull-right dt-margin-left-5 active";
lbListView.Enabled = false;
repGridResults.Visible = false;
repListResults.Visible = true;
}
protected void lbGridView_Click(object sender, EventArgs e)
{
lbListView.CssClass = "btn btn-default btn-sm pull-right dt-margin-left-5";
lbListView.Enabled = true;
lbGridView.CssClass = "btn btn-default btn-sm pull-right dt-margin-left-5 active";
lbGridView.Enabled = false;
repListResults.Visible = false;
repGridResults.Visible = true;
}
Related
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
filldropdown(dllselection.SelectedValue);
Code.Enabled = true;
if(dllselection.SelectedValue=="")
{
Code.Enabled = false;
}
}
}
i think there something wrong with the page load,my 2nd dropdownlist is depend on 1st dropdownlist selection.
<div class="form-group">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label" style="color:black" >Main Category</label>
<div class="col-sm-3">
<asp:DropDownList ID="dllselection" runat="server" CssClass="form-control" AutoPostBack="true" required>
<asp:ListItem Text="Please Select" Value=""></asp:ListItem>
<asp:ListItem Text="HR" Value="M_1"></asp:ListItem>
<asp:ListItem Text="IT" Value="M_2"></asp:ListItem>
<asp:ListItem Text="Maintenance" Value="M_3"></asp:ListItem>
</asp:DropDownList>
</div>
</div>
<div class="form-group">
<label for="Training" style="color:black" class="col-sm-2 control-label">Sub Category</label>
<div class="col-sm-3">
<asp:DropDownList ID="Code" Enabled="false" onchange="javascript:return dropdown(this);" runat="server" CssClass="form-control" ValidationGroup="G1" required></asp:DropDownList>
</div>
</div>
everytime i submit pass data to database,the value for the 2nd dropdownlist always the 1st value.
public void filldropdown(string item)
{
int loggedUserID = Convert.ToInt32(Session["loggedUserID"]);
List<BOL.UserInfo> userslist = new UserInfos().List();
BOL.UserInfo loggeduser = userslist.Where(x => x.UserID == loggedUserID).FirstOrDefault();
// int ID = 10;
List<e_request> role = new e_requests().dropdownlistG(loggeduser.SUBSIDIARY_CD, item);
Code.DataSource = role;
Code.DataTextField = "CAT_DESC";
Code.DataValueField = "SUB_CAT";
Code.DataBind();
}
You can filling drop down on postback which you should not if you want to keep selection. use !Page.IsPostBack instead of Page.IsPostBack
Change
if (Page.IsPostBack)
{
To
if (!Page.IsPostBack)
{
On more thing you may need to put condition out side !Page.IsPostBack as you would need it to be executed on postback
if (!Page.IsPostBack)
{
filldropdown(dllselection.SelectedValue);
}
Code.Enabled = true;
if(dllselection.SelectedValue=="")
{
Code.Enabled = false;
}
Also note you may need to fill the second dropdown on SelectedIndexChange of dllselection and need to set AutoPostBack of dllselection true.
Try this:
if (!IsPostBack)
{
if (dllselection.SelectedValue == "")
{
Code.Enabled = false;
}
else
{
Code.Enabled = true;
filldropdown(dllselection.SelectedValue);
}
}
Try to Load Method in
if (!Page.IsPostBack)
{
filldropdown(dllselection.SelectedValue);
Code.Enabled = true;
if(dllselection.SelectedValue=="")
{
Code.Enabled = false;
}
}
Update:
you need to fill the second dropdown on SelectedIndexChange of dllselection and need to set AutoPostBack = true of dllselection .
<asp:DropDownList ID="logList" runat="server" AutoPostBack="True"
onselectedindexchanged="itemSelected">
</asp:DropDownList>
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack){
filldropdown(dllselection.SelectedValue);
Code.Enabled = true;
if(dllselection.SelectedValue=="")
{
Code.Enabled = false;
}
}
}
add OnSelectedIndexChanged="dllselection_SelectedIndexChanged" to my 1st dropdownlist.
protected void dllselection_SelectedIndexChanged(object sender, EventArgs e)
{
if (dllselection.SelectedIndex == 0)
{
Code.Enabled = false;
}
else
{
Code.Enabled = true;
//fill Code
filldropdown(dllselection.SelectedValue);
}
}
I'm trying to show controls on my webpage when someone clicks a NewUser checkbox but I cant get it working.
Webpage Screenshot
This is my Login.aspx code:
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<div class="checkbox">
<asp:CheckBox runat="server" ID="NewUser" OnCheckedChanged="UpdateOptions" />
<asp:Label runat="server" AssociatedControlID="NewUser">New User?</asp:Label>
</div>
</div>
</div>
This is my Login.aspx.cs code:
//DM Create event to make controls visible if check box is selected
public void UpdateOptions(object update, EventArgs e)
{
if(NewUser.Checked == true)
{
ConfirmEmailLabel.Visible = true;
TextBox3.Visible = true;
ConfirmPasswordLabel.Visible = true;
TextBox4.Visible = true;
Firstname.Visible = true;
TextBox1.Visible = true;
Lastname.Visible = true;
TextBox2.Visible = true;
}
else
{
ConfirmEmailLabel.Visible = false;
TextBox3.Visible = false;
ConfirmPasswordLabel.Visible = false;
TextBox4.Visible = false;
Firstname.Visible = false;
TextBox1.Visible = false;
Lastname.Visible = false;
TextBox2.Visible = false;
}
}
Any ideas would be much appreciated?
Add this
<asp:CheckBox runat="server" ID="NewUser" OnCheckedChanged="UpdateOptions" AutoPostBack="true"/>
You should set true AutoPostBack property of checkbox.
<asp:CheckBox runat="server" AutoPostBack="True" ID="NewUser" OnCheckedChanged="UpdateOptions" />
I am trying to create a simple form generating webpage that user can select different controls via a DropDownList, add a Text property to it(and a GroupName property if ii was a RadioButton) and then click a button to generate that control right in the page for previewing purpose (I am also going to add another button to insert the form's information in a database but this is not my problem right now)
this is my aspx page:
<div class="main-wrapper">
<form dir="rtl" id="form" runat="server">
<label>Desired Control : </label>
<asp:DropDownList AutoPostBack="true" ID="ddlControlType" OnSelectedIndexChanged="ddlControlType_SelectedIndexChanged" runat="server">
<asp:ListItem Text="label"></asp:ListItem>
<asp:ListItem Text="text box"></asp:ListItem>
<asp:ListItem Text="check box"></asp:ListItem>
<asp:ListItem Text="radio button"></asp:ListItem>
</asp:DropDownList>
<br /><br />
<label class="label">control's text : </label>
<asp:TextBox ID="txtboxText" runat="server" CssClass="txtbox"></asp:TextBox>
<label class="label">group name (only for radio button)</label>
<asp:TextBox Enabled="false" ID="txtboxGroupName" runat="server" CssClass="txtbox"></asp:TextBox>
<br /><br />
<asp:Button ID="btnAddControl" CssClass="btn" Text="add to form" OnClick="btnAddControl_Click" runat="server" />
<hr />
<h1>form preview</h1>
<asp:Panel ID="panel" runat="server">
</asp:Panel>
</form>
this is the code behind:
public Dictionary<string, Type> ControlTypes
{
get { return (Dictionary<string, Type>)Session["ControlTypes"]; }
set { Session["ControlTypes"] = value; }
}
public int NumberOfControls
{
get { return (int)ViewState["NumOfControls"]; }
set { ViewState["NumOfControls"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.NumberOfControls = 0;
ControlTypes = new Dictionary<string, Type>();
}
else
this.CreateControls();
}
private void CreateControls()
{
for (int counter = 0; counter < this.NumberOfControls; counter++)
{
string controlId = "control_id_" + counter.ToString();
Type controlType = ControlTypes[controlId];
PropertyInfo[] properties = controlType.GetProperties();
object controlObject = Activator.CreateInstance(controlType);
foreach (var property in properties)
{
if (property.Name == "ID")
property.SetValue(controlObject, controlId, null);
}
panel.Controls.Add(controlObject as Control);
}
}
protected void ddlControlType_SelectedIndexChanged(object sender, EventArgs e)
{
switch (ddlControlType.SelectedIndex)
{
case 0:
case 2:
txtboxText.Enabled = true;
txtboxGroupName.Enabled = false;
break;
case 1:
txtboxText.Enabled = false;
txtboxGroupName.Enabled = false;
break;
case 3:
txtboxText.Enabled = true;
txtboxGroupName.Enabled = true;
break;
}
}
protected void btnAddControl_Click(object sender, EventArgs e)
{
Control tempControl = null;
switch (ddlControlType.SelectedIndex)
{
case 0:
tempControl = new Label();
((Label)tempControl).Text = txtboxText.Text;
break;
case 1:
tempControl = new TextBox();
break;
case 2:
tempControl = new CheckBox();
((CheckBox)tempControl).Text = txtboxText.Text;
break;
case 3:
tempControl = new RadioButton();
((RadioButton)tempControl).Text = txtboxText.Text;
((RadioButton)tempControl).GroupName = txtboxGroupName.Text;
break;
}
string controlId = "control_id_" + this.NumberOfControls.ToString();
tempControl.ID = controlId;
panel.Controls.Add(tempControl);
this.NumberOfControls++;
ControlTypes.Add(controlId, tempControl.GetType());
}
as you can see, I preserve the id's of the controls, but the viewstate won't update their values.the controls will be added to the form with the desired id, but its values won't be set at all
what should I do?
I have a bunch of buttons being dynamically created and setting its click event to an event handler...When I run it they handler in never firing, I've stepped through the code and nothing seems out of the ordinary but it just won't fire.
Here is a snippet of the code where the button is being created..
for (cellCtr = 1; cellCtr <= 9; cellCtr++)
{
Button button = new Button();
HyperLink link = new HyperLink();
TableCell tCell = new TableCell();
if (rowCtr == rN)
break;
string myStr = daccess.dsSubjects.Tables[0].Rows[rowCtr]["SubjectName"].ToString();
int myID = Convert.ToInt32(daccess.dsSubjects.Tables[0].Rows[rowCtr]["SubjectID"].ToString());
button.ID = Convert.ToString(myID);
button.Text = myStr;
button.CssClass = "btn btn-primary btn-sm";
button.Click += ButtonSubjectClick;
tCell.Controls.Add(button);
tRow.Cells.Add(tCell);
rowCtr++;
if (cellCtr == rN)
{
rowCtr = rowCtr - 1;
break;
}
}
and here is the handler that's it suppose to call...
void ButtonSubjectClick(object sender, EventArgs e)
{
Button button = (Button)sender;
daccess.DeleteSubjects(Int32.Parse(button.ID.ToString()));
button.CssClass = "btn-active";
}
When its ran, the source looks like this
<input type="submit" name="38" value="Math" id="38" class="btn btn-primary btn-sm" />
This code works for me:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// do something if needed
}
// outside of postback, you need to add it each time
Button button = new Button();
button.ID = "btn1";
button.Text = "clicky me";
button.Click += new EventHandler(ButtonSubjectClick);
pnl.Controls.Add(button);
}
void ButtonSubjectClick(object sender, EventArgs e)
{
Button button = (Button)sender;
button.Text = "clicked me";
}
I believe it's a question about how are you adding your button to the page. I'm adding my button at each postback and it is thus registered by the server.
If you are adding your buttons in another event then you could use viewstate or session variables, store some kind of a flag to create the button and then check for this flag in each postback.
Edit, for the sake of clarity, pnl is a Panel on my page:
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnl" runat="server"></asp:Panel>
</div>
</form>
I have referred Error with the event handlers of dynamic linkbutton . It says to add event handlers in Page_Init or Page_Load. I tired following code. But the event handler is not fired when I click on the dynamic added link buttons. What need to be corrected here?
Note: The dynamic LinkButton controls are added in the click event of a button after some business validations (the business code is not given for brevity)
Markup
<form id="form1" runat="server">
<div>
<asp:LinkButton ID="lnkTest" runat="server" OnClick="LinkButton_Click">Static LinkButton</asp:LinkButton>
<br />
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
<br />
<asp:PlaceHolder ID="plhDynamicLinks" runat="server"></asp:PlaceHolder>
</div>
</form>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
foreach (Control ctrl in plhDynamicLinks.Controls)
{
LinkButton dynamicButton = (LinkButton)ctrl;
dynamicButton.Click += new EventHandler(LinkButton_Click);
}
if (Page.IsPostBack)
{
}
}
protected void Page_Init(object sender, EventArgs e)
{
int x = 0;
foreach (Control ctrl in plhDynamicLinks.Controls)
{
LinkButton dynamicButton = (LinkButton)ctrl;
dynamicButton.Click += new EventHandler(LinkButton_Click);
}
}
protected void LinkButton_Click(object sender, EventArgs e)
{
LinkButton clickedControl = (LinkButton)sender;
Response.Write(clickedControl.ID +" Link Button Clicked");
}
protected void btnAdd_Click(object sender, EventArgs e)
{
plhDynamicLinks.Controls.Clear();
LinkButton button1 = new LinkButton();
button1.ID = "D1";
button1.Text = "1";
plhDynamicLinks.Controls.Add(button1);
LinkButton button2 = new LinkButton();
button2.ID = "D2";
button2.Text = "2";
plhDynamicLinks.Controls.Add(button2);
}
It is mandatory to register all the required dynamic controls’ event handlers in the Page_Load/ Page_Init itself. One working example can be seen at Dynamic Control’s Event Handler’s Working
MarkUp
<form id="form1" runat="server">
<div>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
<br />
<asp:PlaceHolder ID="plhDynamicLinks" runat="server"></asp:PlaceHolder>
</div>
</form>
CODE BEHIND
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
LinkButton lnk1 = new LinkButton();
lnk1.ID = "D1";
lnk1.Text = "A";
//Event handler must be registered in the Page_Load/Page_Init
lnk1.Click += new EventHandler(LinkButton_Click);
plhDynamicLinks.Controls.Add(lnk1);
LinkButton lnk2 = new LinkButton();
lnk2.ID = "D2";
lnk2.Text = "B";
lnk2.Click += new EventHandler(LinkButton_Click);
plhDynamicLinks.Controls.Add(lnk2);
LinkButton lnk3 = new LinkButton();
lnk3.ID = "D3";
lnk3.Text = "C";
lnk3.Click += new EventHandler(LinkButton_Click);
plhDynamicLinks.Controls.Add(lnk3);
LinkButton lnk4 = new LinkButton();
lnk4.ID = "D4";
lnk4.Text = "D";
lnk4.Click += new EventHandler(LinkButton_Click);
plhDynamicLinks.Controls.Add(lnk4);
}
}
protected void LinkButton_Click(object sender, EventArgs e)
{
PopulateLinksBasedOnCriteria();
LinkButton clickedControl = (LinkButton)sender;
Response.Write(DateTime.Now.ToString()+"___"+ clickedControl.ID + " Link Button Clicked" );
}
protected void btnAdd_Click(object sender, EventArgs e)
{
PopulateLinksBasedOnCriteria();
}
private void PopulateLinksBasedOnCriteria()
{
plhDynamicLinks.Controls.Clear();
if (DateTime.Now.Second < 30)
{
LinkButton linkButton1 = new LinkButton();
linkButton1.ID = "D1";
linkButton1.Text = "1";
plhDynamicLinks.Controls.Add(linkButton1);
LinkButton linkButton2 = new LinkButton();
linkButton2.ID = "D2";
linkButton2.Text = "2";
plhDynamicLinks.Controls.Add(linkButton2);
}
else
{
LinkButton linkButton3 = new LinkButton();
linkButton3.ID = "D3";
linkButton3.Text = "3";
plhDynamicLinks.Controls.Add(linkButton3);
LinkButton linkButton4 = new LinkButton();
linkButton4.ID = "D4";
linkButton4.Text = "4";
plhDynamicLinks.Controls.Add(linkButton4);
}
}
Dynamic controls must be re-created on every postback, this Article is a good link about how to persist dynamic controls and their state.
Add javascript onClick attribute to the dymanic control and set hidden field values which is required for the control event. Onclick of the dymanic grid, the will postback and will get the hidden field value. In page load call a method to do the job if the hidden field has value and make it null after doing the job.