trigger an eventhandler on postback c# - c#

I am pretty new to c# programming.
I have an issue that follows:
I have a GridView on a web form, a DropDownList and a Label control. When i select a value from a DDL a Grid View is populated with rows from the database that equal the DDL condition(in my case DDL represent Countries, GV lists the Cities).
When i delete the last City from a GV using a built in GV Delete function i would like to automatically write in a Label that there are no more Cities in selected Country.
How can i achieve that?
I tried to put
protected void GridView1_RowDeleted1(object sender, GridViewDeletedEventArgs e)
{
if (GridView1.Rows.Count == 0)
{
LabelGrid.Text = "No more cities.";
}
}
but it didn't work.
Thanks for your help

Consider using PreRender event, when that event runs the Row.Count property contains the correct value. (decremented after delete)
protected void GridView1_PreRender(object sender, EventArgs e)
{
if (GridView1.Rows.Count == 0)
{
LabelGrid.Text = "No more cities.";
}
}

Related

Is there a way of change values within a DetailView from a selected it value from GridView?

I would like to display the correct object Customer in the DetailView. The problem is that because DetailView is created at the design time, it always displays the first Customer object from my table, not the selected one from the GriedView.
Does it exist something like this?
Thank you!!
protected void grdvReceipt_SelectedIndexChanged(object sender, EventArgs e)
{
if (grdvReceipt.SelectedIndex != -1)
{
//Display Customer in DetailView by updating the ObjectDataSource
this.dtvCustomerFullDetail.ObjectDataSource['id']=grdvReceipt.SelectedRow.Cells[7].Text;
}
}

Checkboxlist loop is not working

I have a dropdownlist control and a button in asp.net page. The dropdownlist is populated from a method. If I select any item other than the first item, after clicking the button, I lose the selected item in the DDL and it selects the first item and also I am getting the value of the first item only in the button click event. How can I fix the problem?
<asp:DropDownList ID="userDropDown" runat="server" DataTextField="CustomerName" DataValueField="CustomerId">
</asp:DropDownList>
protected void Button1_Click(object sender, EventArgs e)
{
if(!page.isPostBack)
{
userDropDown.DataSource = CC.GetCustomers();
userDropDown.DataBind();
}
}
i think you must have bind userDropDown in Page_Load event without condition
if (!IsPostBack)
Please put dropdown binding part inside if (!IsPostBack) condition then it should work
Please bind dropdownlist values inside the if(!ispostback){} or
after submitting button please bind updated field to dropdownlistname.text
It sounds like you are binding your DropdownList to your datasource at ever request. Instead bind it only if Page.IsPostBack is false like below; (You may not need ObjectDataSource)
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//bind your datasource here (something like below)
userDropDown.DataSource = GetCustomers();
userDropDown.DataBind();
}
}
As soon as DataBind() method is called it will lose posted data of that object and the FirstItem will be selected by default.

How do I Grab Selected Index on two Dropdownlist's OnSelectedIndexChanged?

I am trying to use two DropDownLists to filter data. I set both of the OnSelectedIndexChanged Equal to the method below. The problem is it is only grabbing the SelectedIndex of the DDL that is changed. Example: if i choose a option in DDL1 it grabs that value and doesnt grab the value of DDL2. They both have the same OnSelectedIndexChanged i figured it would grab the current value of both. Is there a way to make it look at both DDL Controls?
protected void BrandsList_SelectedIndexChanged(object sender, EventArgs e)
{
int DDLcatId = CategoriesList.SelectedIndex;
int DDLBraId = BrandsList.SelectedIndex;
IQueryable<Product> DDLprodResult = GetProductsDDL(DDLcatId, DDLBraId);
if(DDLprodResult == null)
{
}
else
{
CatLab.Text = DDLprodResult.ToList().Count().ToString();
productList.DataSource = DDLprodResult.ToList();
productList.DataBind();
}
}
Your code should work. Of course only one can be changed if you have set AutoPostBack="true"(default is false) on both. But you should get the correct SelectedIndex anyway in the handler.
So i will guess: you are databinding the DropDownLists on every postback. Do this only if(!IsPostBack), otherwise you always overwrite changes with the original values.
So for example in Page_Load:
protected void Page_Load(Object sender, EvengtArgs e)
{
if(!IsPostBack)
{
// DataBind your DropDownLists
}
}

Accessing gridview from a non GridView event

I have a button outside a gridview called "UpdateAll", i need to be able to click on such button and find an item template "DropDownList" and update the database with that value for each record, I am not sure how to go about this any ideas?
public void UpdateAll_Click(object sender, EventArgs e)
{
}
Now I know I can access the drop down in the GridView_RowCommand something like this
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
Myddl = row.FindControl("Quantity") as DropDownList;
But i am not sure how to do that from an outside event that is not GridView related, I can not do the one above because it accesses the e.CommandSource.
Please help.
You can do something like this;
for(int rowIndex =0; rowIndex<gv.rows.count; rowIndex++)
{
Myddl = gv.rows[rowIndex].FindControl("Quantity") as DropDownList;
}

GridView selects wrong row for editing

I have a simple admin panel with a simple gridview that list all articles present in the database.
I've added a filtering doodle (a textbox + a button) that allows the user to filter the gridview by a article name.
The code for the filter:
protected void ButtonSearchArticle_Click(object sender, EventArgs e)
{
{
LinqDataSourceAdminArticles.Where = "Title.Contains(" + "\"" + TextBoxSearchArticle.Text + "\")";
LinqDataSourceAdminArticles.DataBind();
}
LinqDataSourceAdminArticles.DataBind();
}
The gridview has the default quick editing and deleting enabled on it. The problem is, after I filter it with that code, it starts to select wrong rows when I click the "edit" button. Any ideas how to solve this? I know it has something to do with a postback.
I've checked Why is My GridView FooterRow Referencing the Wrong Row? and Sorted gridview selects wrong row but those didn't solve my problem.
Thanks!
When you change the GridView's Select query in your button click, it only take effects for that request. because GridView's edit command causes a postback, and in the postback the Gridview works with visible index of the edited row but without the filtering.
the best thing to do is to remove your ButtonSearchArticle_Click code and put it into your Page_Load code like this
if (TextBoxSearchArticle.Text != ""){
LinqDataSourceAdminArticles.Where = "Title.Contains(" + "\"" + TextBoxSearchArticle.Text + "\")";
LinqDataSourceAdminArticles.DataBind();
LinqDataSourceAdminArticles.DataBind();}
//Your Page Load Event
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["s_event"] = "0"; // Initialize session variable
BindData(); // Gridview Binding
}
}
protected void BindData()
{
if ((Session["s_event"].ToString())=="1")
{
cmdstr_ = (Session["search_item"].ToString());
}
else
{
cmdstr_ = ""; // Your command string to populate gridview
}
// `enter code here`
}
protected void btnSearch_Click(object sender, EventArgs e)
{
Session["s_event"] = 1; // Search Event becomes 1.
// Your Search Logic here
Session["search_item"] = cmdstr;
// Bind Gridview here
}

Categories

Resources