I have a series of checkboxes in an ASP.Net page. Every checkbox makes call to single function that accepts the checkbox id as a parameter.
The problem is that the code is now redundant. Is there a way to generalize the code. Attaching the sample code below:
protected void chkAddlDetails_CheckedChanged(object sender, EventArgs e)
{
if (chkAddlDetails.Checked == true)
{
blnShouldLock("chkAddlDetails");
Panel9.Visible = true;
}
else
{
Panel9.Visible = false;
}
}
protected void chkDeValidate_CheckedChanged(object sender,EventArgs e)
{
if (chkDeValidate.Checked == true)
{
blnShouldLock("chkDeValidate");
Panel10.Visible = true;
}
else
{
Panel10.Visible = false;
}
}
............
............ and similar code repeats for other check boxes
As the checkbox count is increasing the code is expanding and getting more redundant.
Can anyone assist to have a generalized code to avoid redundancy. Appreciate if a pseudo code can be provided.
Thank you.
Use the same event for all your checkboxes. You can then cast sender to the CheckBox that fired the event.
If each panel corresponds to a single checkbox, you can name the panels with the names of the checkboxes and suffix them with "Panel". For example a checkbox called chkDevalidate could have a panel called chkDevalidatePanel and the below method should work for all your checkboxes. If the same panel is shown or hidden by multiple checkboxes you'll need to map them between each other using a Dictionary or similar.
protected void GenericCheckedChanged(object sender, EventArgs e)
{
CheckBox checkbox = (CheckBox)sender;
Panel panel = (Panel)Page.FindControl(checkbox.ID + "Panel");
if (checkbox.Checked)
{
blnShouldLock(checkbox.ID);
panel.Visible = true;
}
else
{
panel.Visible = false;
}
}
or without an if.
panel.Visible = checkbox.Checked
Related
I am disabling a checkbox column in a XtraGrid GridView with the following code (works as expected). Got the code from this post https://www.devexpress.com/Support/Center/Question/Details/Q423605:
private void GridViewWeeklyPlan_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
{
if (e.Column.FieldName == "Ignore")
{
CheckEditViewInfo viewInfo = ((GridCellInfo)e.Cell).ViewInfo as CheckEditViewInfo;
viewInfo.CheckInfo.State = DevExpress.Utils.Drawing.ObjectState.Disabled;
}
}
ISSUE
I want to enable the checkbox again when a certain column changes and has a value. This is where I am stuck and I thought I can change it in the GridView's CellValueChanged event, but I do not know how to reference the cell/column for the row:
private void GridViewWeeklyPlan_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
if (e.Column.FieldName != "Reason") return;
if (String.IsNullOrEmpty(e.Value.ToString()))
{
//Make sure the checkbox is disabled again
}
else
{
//Enable the checkbox to allow user to select it
}
}
You need to refresh a cell in the Ignore column. You can do this by calling the GridView.RefreshRowCell method. To identify a row that you need to refresh, the CellValueChanged event provides the e.RowHandle parameter.
private void GridViewWeeklyPlan_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
if (e.Column.FieldName != "Reason") return;
GridView view = (GridView)sender;
view.RefreshRowCell(e.RowHandle, view.Columns["Ignore"]);
}
The CustomDrawCell event will be raised again to update the cell appearance.
I have a project that using dropdownlist for choices.When check checkbox1 dropdown automatically bind data from database using table1 and when I check checkbox2 dropdown automatically binding data from database using table2.I do not want to use get data by using any button .How can I do that .Please help me.
here is code by using button:
public void LokasyonDoldur()
{
birimBUS = new BirimBUSV1();
List<BirimVO> birimVO = new List<BirimVO>();
DrpChcs.Items.Clear();
List<ListItem> items = new List<ListItem>();
birimVO = birimBUS.LokasyonlariGetir();
foreach (var item in birimVO)
{
items.Add(new ListItem(item.BirimAdi, item.ID.ToString()));
}
DrpChcs.Items.AddRange(items.ToArray());
}
public void BirimleriDoldur()
{
PoliklinikBUS poliklinikBUS = new PoliklinikBUS();
List<PoliklinikVO> poliklinikVO = new List<PoliklinikVO>();
DrpChcs.Items.Clear();
List<ListItem> items = new List<ListItem>();
poliklinikVO = poliklinikBUS.Poliklinikler();
foreach (var item in poliklinikVO)
{
items.Add(new ListItem(item.PoliklinikAdi, item.ID.ToString()));
}
DrpChcs.Items.AddRange(items.ToArray());
}
protected void BtnLokasyon_Click(object sender, EventArgs e)
{
if (ChckLctn.Checked == true && ChckBrm.Checked==false)
{
LokasyonDoldur();
}
else if (ChckLctn.Checked == false && ChckBrm.Checked == true)
{
BirimleriDoldur();
}
else
{
}
Button1.Visible = true;
BtnLokasyon.Visible = false;
}
protected void DrpChcs_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
KirilimId = Int32.Parse(DrpChcs.SelectedValue);
BPolikilinikID= KirilimId;
}
but I do not want to use this one.
ohh its another language. its hard to read. but what you basicly have to do is check which checkbox is checked in the page load and then load the dropdown based on what is loaded.
something like this. (I have typed it from my head so its not like copy-paste but you get the idea)
page_load
{
if(checkbox1.checked)
{
dropdown.dataitems = items1;
dropdown.databind();
return;
}
if(checkbox2.checked)
{
dropdown.dataitems = items2;
dropdown.databind();
return;
}
}
YOu can call the Button1_click event from the Dropdown list selected index changed event like this
Button1_Click(Button1,new EventArgs());
and in this you can hide that button from the page and in code behind you are calling the same function
OR
You can refactor the code in a seperate function from the button click event and call that function in the selected index changed event.
Please let me knwo if I misunderstood your question
Thanks
How can i make a button work such that it clears up all text boxes of pop up at a same time ...
Is there a single line of code which can perform the above function?
I tried this
protected void AddNext_Click(object sender, EventArgs e)
{
AccountNumber.Text = null;
AccountTitle.Text = null;
PinNumber.Text = null;
CreationDate.Text = null;
Balance.Text=null;
AccountTitle.Text = null;
}
But is there a better way to Get all text boxes blank ??
this works if you want ALL textboxes on your page to be cleared on the click of a button:
protected void Button1_Click(object sender, EventArgs e)
{
foreach (TextBox i in this.Page.Form.Controls.OfType<TextBox>().ToList())
{
i.Text = null;
}
}
i'd be careful with this tho, as it will clear every textbox that is located on your page. But apart from that, this does what you asked for.
Find the form it's in and call reset() function on it in JavaScript onclick of button.
I have a ListView in an ASP.NET web application. When a user clicks the edit button, I want textfields to pop up that are dependent on certain values of the item. However, I can't seem to find any controls inside of my ListView1_ItemEditing() function.
I have read the Microsoft documentation and various help threads on the internet, but their suggestions do not appear to work for me. This is generally what I see:
ListViewItem item = ProductsListView.Items[e.NewEditIndex];
Label dateLabel = (Label)item.FindControl("DiscontinuedDateLabel");
For the sake of simplicity I just want to be able to select a label in ListView1_ItemEditing(). This is the code in ListView1_ItemEditing():
protected void ListView1_ItemEditing(Object sender, ListViewEditEventArgs e)
{
DataBind(); //not sure if this does anything
ListViewItem item = ListView1.Items[e.NewEditIndex];
Label debugLabel = (Label)item.FindControl("label_editing");
debugLabel.Text = "Works";
}
Here is the ASP
<EditItemTemplate>
<asp:Label ID="label_editing" runat="server" Text="hello world"></asp:Label>
</EditItemTemplate>
When debugging, item and debugLabel are both NULL.
UPDATE: I resolved this issue by moving my logic to ItemDataBound and then checking if my tr (containing textboxes) was in that particular data item. Code below:
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Control tr_verizon = e.Item.FindControl("tr_verizonEdit");
Control tr_att = e.Item.FindControl("tr_attEdit");
if (tr_verizon != null)
{
//Control tb_meid = e.Item.FindControl("TextBox_Meid");
Label lbl_carrierId = (Label)e.Item.FindControl("lbl_carrierId");
if (lbl_carrierId == null)
{
Message.Text = "lbl_carrierId is null!";
}
else if (lbl_carrierId.Text.Equals(""))
{
Message.Text = "lbl_carrierId is empty!";
}
else
{
string recordId = lbl_carrierId.Text;
if (tr_verizon != null && tr_att != null)
{
if (lbl_carrierId.Text.Equals("1"))
{
tr_verizon.Visible = false;
tr_att.Visible = true;
}
else
{
tr_verizon.Visible = true;
tr_att.Visible = false;
}
}
}
}
}
}
The ItemEditing event is raised when an item's Edit button is clicked, but before the ListView item is put in edit mode. Therefore controls in EditItemTemplate are not available at this time.
More Info and example
You should do the DataBind() first, like this:
ListView1.EditIndex = e.NewEditIndex;
ListView1_BindData(); // a function that get the DataSource and then ListView1.DataBind()
// Now find the control as you did before
Have you tried casting the sender object instead of trying to access your ListViewItem by index?
protected void ListView1_ItemEditing(Object sender, ListViewEditEventArgs e)
{
var item = sender as ListViewItem;
var debugLabel = item.FindControl("label_editing") as Label;
debugLabel.Text = "Works";
}
I have SqlDataSource and GridView on web form. GridView.DataSourceID = mySqlDataSource.
When I call myGridView.DataBind(), all data successfully bind on the page.
How is it possible to read already got data from mySqlDataSource or myGridView objects as DataTable or DataView? Thanks
The data in the gridview can read by using FindControl property of Gridview control. For example, for reading values set in the Checkbox column in the grid.
for (i = 0; i < GridView1.Rows.Count; i++)
{
CheckBox chk = (CheckBox)GridView1.Rows[i].FindControl("checkbox1");
//here code for using value captured in chk
}
Provided your data set isn't gigantic, you could store it in the Session, bind your GridView to the Session data, and then re-use the Session data in your other web objects.
Your code would then look something like this (you'll have to forgive any minor inaccuracies -- I'm away from my development box at the moment):
protected override OnInit(object sender, EventArgs e)
{
base.OnInit();
if (Page.IsPostback == false)
{
SetSessionData();
}
//
// Set your GridView, DataTable, DataView, etc. events here.
//
}
void SetSessionData();
{
List<YourDataBoundObject> myDataBoundObject = GetYourDataBoundObject(); // Or Collection<T>, IEnumerable<T>, etc.
Session["data"] = myDataBoundObject;
}
void YourGridView_Load(object sender, EventArgs e)
{
BindYourGridView();
}
void BindYourGridView()
{
YourGridView.DataSource = GetSessionData();
YourGridView.DataBind();
}
List<YourDataBoundObject> GetSessionData()
{
return (List<YourDataBoundObject>) Session["data"];
}
void YourDataTable_Load(object sender, EventArgs e)
{
BindYourDataTable();
}
void BindYourDataTable()
{
YourDataTable.DataSource = GetSessionData();
YourDataTable.DataBind();
}