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
}
Related
I want to filter the items that I added to my Listview, using my textbox_TextChanged Can you please show me the Codes, I am using Visual Studio 2013. tia
e.g.
First I am adding a Destination/Regularfare/Discountedfare/Baggagefare to my ListView. and my problem is Searching I want to search the Destinations using a TextBox.
There are no Codes inside my TextBox Search. that one I need.
And here's my Codes for adding an item to my ListView.
public void add(String destination, String Regulare, String Discounted, String Baggage) {
String [] rows = { destination, Regulare, Discounted, Baggage};
ListViewItem item = new ListViewItem(rows);
listView1.Items.Add(item);
}
private void btnAdd_Click(object sender, EventArgs e) {
add(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
textBox1.Text = "";
textBox2.Text = "0";
textBox3.Text = "0";
textBox4.Text = "0";
MessageBox.Show("Record Added!","Saved",MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
tbDestination.Focus();
}
Like the other posters were saying, we don't know what your tech stack is or what problem you're trying to solve. Have you tried binding your list to a data source? Let's say you have a text box and a submit button for filtering. The user enters "California" into the text box and presses submit. You'll have a click handler in the code behind that invokes a DataBind() on your list. The method for your data bind will get the value of the textbox/hidden field and use it to return a conditional data structure that is then used to populate your list. You could even have a clear button that resets the filtering using the same method.
But it's been 16 days now, so you've probably already solved the problem. What did you end up doing?
Hı , Try this two way
First :
private void searchBox_TextChanged(object sender, EventArgs e)
{
// Call FindItemWithText with the contents of the textbox.
ListViewItem foundItem =
textListView.FindItemWithText(searchBox.Text, false, 0, true);
if (foundItem != null)
{
textListView.TopItem = foundItem;
}
}
Second :
void yourListView_MouseDown(object sender, MouseEventArgs e)
{
// Find the an item above where the user clicked.
ListViewItem foundItem =
iconListView.FindNearestItem(SearchDirectionHint.Up, e.X, e.Y);
if (foundItem != null)
previousItemBox.Text = foundItem.Text;
else
previousItemBox.Text = "No item found";
}
So, when I click Select I want to show in a Label all datas that contain one row. I have managed to make this, except DropdownList. When I click "Select" it's just empty.
protected void GridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
Label1.Text = GridView1.Rows[e.NewSelectedIndex].Cells[1].Text;
Label2.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
Label3.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
}
P.S.: I have not done this programmatically. The only code I've wrote on .aspx.cs file is the code above.
Use this to find the value. its not showing the value because of template field control.
I have used the gridview control that you pasted in pastebin.
protected void GridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) {
Label drpValue =
(Label)this.GridView1.Rows[e.NewSelectedIndex].Cells[1].FindControl("Label1");
Lbl1.Text = drpValue.Text;
Lbl2.Text = GridView1.Rows[e.NewSelectedIndex].Cells[2].Text;
Lbl3.Text = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
}
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.";
}
}
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
}
}
i have a alphabetic filter consist of 26 dynamically created link button on selecting any link button it is filtering the name of user's on the basis of alphabet and changing its color to orange to make it different from other linkbuttons it is working fine but if there are more number of user associated with a particular alphabet and on applying filter it is filtering the user on the basis of that alphabet and showing them in a list view on clicking the data pager next page or any other page number the link button changes its color to default color but i want to keep that highlighted until and unless other link button is selected
my code
protected void Page_Init(object sender, EventArgs e)
{
// Adding Dynamically linkbuttons for all alphabets(i.e. A-Z)
for (char asciiValue = 'A'; asciiValue <= 'Z'; asciiValue++)
{
LinkButton lbtnCharacter = new LinkButton();
lbtnCharacter.ID = "lbtnCharacter" + asciiValue;
divAlphabets.Controls.Add(lbtnCharacter);
// Setting the properties of dynamically created Linkbutton.
lbtnCharacter.Text = Convert.ToString(asciiValue);
lbtnCharacter.CssClass = "firstCharacter";
lbtnCharacter.ToolTip = "Show Tags starting with '" + Convert.ToString(asciiValue) + "'";
lbtnCharacter.CommandArgument = Convert.ToString(asciiValue);
lbtnCharacter.Command += new CommandEventHandler(lbtnCharacter_Command);
}
}
// For assigning default color to linkbutton text in page load
foreach (var ctrl in divAlphabets.Controls)
{
if (ctrl is LinkButton)
((LinkButton)ctrl).CssClass = "firstCharacter";
}
void lbtnCharacter_Command(object sender, CommandEventArgs e)
{
// Storing the values of pressed alphabet in viewstate.
ViewState["Selected_Character"] = e.CommandArgument;
LinkButton lbtnSelected = (LinkButton)divAlphabets.FindControl("lbtnCharacter" + e.CommandArgument);
lbtnSelected.CssClass = "firstCharacter highlighted";
txtTagFilter.Text = string.Empty;
BindTagList();
}
I hope I understood your question.
You are setting your Selected_Character item in the command handler and then setting the class of the button to highlight it. This only gets fired when the button is clicked, not when you move to the next page. Why not separate these two operations. Set the class of the link button on prerender if the Selected_Character matches. That way even when you page the link button will stay highlighted.
I would also set your selected character as a query string parameter, if someone copies and pastes a link to your page the button would not highlight and the correct data would not display.
Hope this helps.
Edit: Haven't tested the below but maybe it will get you started.
void lbtnCharacter_Command(object sender, CommandEventArgs e)
{
// redirect to self with tag as qs parameter
Response.Redirect(string.Format("{0}?tag={1}", Request.Url.GetLeftPart(UriPartial.Path), e.CommandArgument));
}
protected void Page_PreRender(object sender, EventArgs e)
{
if (Request.QueryString["tag"] != null) {
LinkButton lbtnSelected = (LinkButton)divAlphabets.FindControl("lbtnCharacter" + Request.QueryString["tag"]);
lbtnSelected.CssClass = "firstCharacter highlighted";
}
}
N.B You will also need to change your BindTagList to use the query string also. I'm assuming you call this in the page load event.