I'm trying to get a Gridview "select" link to drill down to a specific page for the selected row in ASP.NET with C#. This gridview is generated dynamically in the page_load, and is databound from a fairly simple SQL select query. Each row has a unique id, and I would like this to be passed as a parameter in the url when the select button is clicked for that row. So, when you click the "select" button on the row with an id value of 9 (note - not the id as defined by the gridview, but the one from the SQL query) you are redirected to an address such as moreDetail.aspx?id=9.
However, when trying to pass the id into the event handler I've hit issues... GridView.SelectedIndexChanging takes the usual (object sender, EventArgs e) as parameters and nothing else, and since the Gridview is created at Page_Load the EventArgs class is useless. I can't seem to find any way to pass the id that I have retrieved earlier into the event handler.
After lots of searching,I tried creating a class that extends EventArgs (obviously with my extra parameter added in), but it seems using any parameters other than (object sender, EventArgs e) just won't work. I could theoretically redo the SQL query within the event handler, but that seems to me a terrible way to achieve what I'm looking for, so I'm hoping someone will be able to see what I've got wrong here, because I'm sure I'm missing something obvious.
Some code - grid.SelectedRow.Cells[0] will contain the parameter I want to pass:
In Page_Load:
GridView grid = new GridView();
grid.DataSource = source;
CommandField selectField = new CommandField();
selectField.ShowSelectButton = true;
selectField.SelectText = "View Jobs";
grid.Columns.Add(selectField);
grid.SelectedIndexChanging += grid_SelectedIndexChanging;
grid.DataBind();
content.Controls.Add(grid);
And the Event Handler:
protected void grid_SelectedIndexChanging(object sender, EventArgs e)
{
Response.Redirect("ViewCustomer.aspx?id=" + grid.SelectedRow.Cells[0]);
}
Obviously this doesn't work because the scope of grid doesn't extend to the handler...but how can I get access to that data?
You need to cast sender to GridView to reference your calling GridView:
protected void grid_SelectedIndexChanging(object sender, EventArgs e)
{
GridView grid = (GridView)sender;
Response.Redirect("ViewCustomer.aspx?id=" + grid.SelectedRow.Cells[0]);
}
Related
I have 2 gridviews which update some data, they are independent each one,
both are using "onrowupdating="actualizar"
i want to use the same event like below:
protected void actualizar(object sender, GridViewUpdateEventArgs
e)
{ }
How can i select the specific grid view inside the event?
The object sender will be the GridView who started the event so you can cast it to GridView and you will have it's properties like its Id.
Example:
(sender as GridView).ID
Thats the answer !!
Control cntrl = sender as GridView;
var gridId = cntrl.ID;
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.
I have a code that generates multiple panels, each including some labels and comboboxes. One of the controls included in each panel is a checkbox, what I need to do is, that when user checks the checkbox, the whole panel where the checkbox was will be deleted.
Inside a foreach loop that generates the panels, the panel itself can be reached with name "Strip". Outside this loop, if I would transfer all necessary parameters(or arguments, not sure about the terminology here), it would be called after one of its labels "callsign", so when I need to get its name out into a method, I set as a parameter "callsign.Text".
Now, here is my Checbox generating code:
CheckBox check = new CheckBox();
check.Location = new Point(270, 10);
check.Name = "check:" + callsign.Text;
check.CheckedChanged += new System.EventHandler(CheckCheckedChanged(callsign.Text));
Strip.Controls.Add(check);
and here is definition of method CheckCheckedChanged:
public void CheckCheckedChanged(string callsign, object sender, EventArgs e)
{
}
... First of all, I get en error at line check.CheckedChanged += new System.EventHandler(CheckCheckedChanged(callsign.Text));
It says, that "No overload for method CheckCheckedChanged takes 1 arguments". I dont know whats wrong, so thats my first question. The second is - I cant figure out how to write the method to delete the one specific panel named after the callsign.Text, if I would write just "callsign dispose" then I guess it wouldnt work.
Thanks in advance
The first thing you need to do is remove the string callsign parameter from your event handler. That is what's causing the compiler error. So your method signature will look like this:
public void CheckCheckedChanged(object sender, EventArgs e)
Secondly, you need to change how the event is attached to event handler:
check.CheckedChanged += new System.EventHandler(CheckCheckedChanged(callsign.Text));
will become:
check.CheckedChanged += new System.EventHandler(CheckCheckedChanged);
Next, you want to put code into the event handler to get the state of the checkbox and set the visibility of the panel accordingly. You will replace the panel with the name of your panel.
public void CheckCheckedChanged(object sender, EventArgs e)
{
CheckBox checkbox = sender as CheckBox;
if (checkbox != null)
{
((Panel)checkbox.Parent).Visible = !checkbox.Checked; // replace this with your panel
}
}
consider the following:
code for buttonclick fn in .cs file :
protected void additembtnClick(object sender, EventArgs e)
{
DataTable dt = createTemptable();
dt = (DataTable)Session["dfdtemptable"];
this.DFDLOVlst.DataSource = dt;
this.DFDLOVlst.DataBind();
this.txtLOVCode.Visible = true;
this.txtLOVvalue.Visible = true;
MDIngrdientsCode.Show();
}
this is the on_row clicked fn of the datagrid
protected void OnFindSelect(int Value)
{
}
code for findbtn click:
protected void btnFind_Click(object sender, EventArgs e)
{
this.FindLookup.SetLookup();
this.FindLookup.SearchLookup();
this.MdFindLookup.Show();
}
When the find button is clicked,the lookup control gets loaded with values from database.On selecting any row the selected row values will be added to corresponding text boxs and data grid.
The additembtnclick is to add new entry to the datagrid.The user can add new values or can click the find and select a row and update its values in the datagrid.
Theres no problem when the user adds fresh values .When the user click the find and select a particular row.The values gets add to correspondind fieldls..And now if the user clicks the additem button to add new values to already existing values in datagrid then the fn onfindselected(fn for the onrowclicked event for the lookup control containg datagrid) is called automatically,but the call is not in the button click fn..
Can't figure out whats wrong..?
It appears that theOnFindSelect is not an event handler itself. It is probably being called from another event handler (or more than 1). Try looking for all calls to it and see if that makes sense
How can one clear SELECT parameters of an SQL Data Source after a GridView (bound to the said SQL Data Source) is rendered?
Edit:
I would like to clear the SELECT parameters of a SQL Data Source after the data which was selected by the SQL Data Source has been displayed in a GridView, but clearing the parameters in the functions associated with the GridView DataBound and SQL Data Source Selected events results in a "Must declare the scalar variable" error, suggesting that the parameters were not defined when the SELECT query was executed. When is the appropriate time to clear the SELECT parameters?
Consider the following code, where "Selected" is associated with the SQL Data Source Selected event which is fired after a select command is completed. This results in a "Must declare the scalar variable" error.
static bool clearParams;
protected void Selected(object sender, EventArgs e) {
if (clearParams)
{
SqlDataSource1.SelectParameters.Clear();
clearParams = false;
}
}
protected void Button_Click(object sender, EventArgs e){
SqlDataSource1.SelectCommand = [a dynamically generated select command];
SqlDataSource1.SelectParameters.Add([various params are added]);
clearParams = true; // clear params after this select command
SqlDataSource1.Select(new DataSourceSelectArguments());
}
GridView has a DataBound event which runs once after the data has been bound, RowDataBound event is running for each individual row. So you can handle the DataBound event and t will be enough