ObjectDataSource - how can I bind only if button pressed - asp.net

I have a button, which when presses populates a grid with data. If I add an ObjectDataSource, and bind the grid to it, it will populate the grid when the page loads. But I need to populate the grid only if the button is pressed, because it is a lengthy opperation. How should I accomplish this

add an event handler to ObjectDataSource's Selecting event like this:
protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
if (!IsPostBack)
{
e.Cancel = true;
}
}
and put a button on page, when it was clicked a postback will occur and ObjectDataSource will return data successfully.

Related

Posted data in gridview where I was added control dynamically

I added controls (i.e. template column) dynamically in grid view and click on button (post back) then grid view populated existing data (i.e.posted data) twice separated by
protected void Page_Init(object sender, EventArgs e)
{
//on init recreated controls
// if (IsPostBack)
{
gvFirst.DataSource = GetData("select top 10 * from Project_Master");
gvFirst.DataBind();
}
}
Sounds like you might be using Auto-generated fields. In design view, click the smart tag on the gridview and click "edit columns." Then uncheck the checkbox that says "Auto-generate fields." I think this should fix your problem if I am understanding you correctly.

Missing selected value from RadioButtonList after PostBack

First the page loads and shows an empty textbox and a button "GO"
Upon clicking the button "GO" a radioButtonList already in the page is loaded from a table based on the text in the textbox.
The radioButtonList is shown with new button "Made my choice".
The user chooses a button and clicks "Made my choice".
Upon checking for selected value or index the radiobuttonList is not checked at all...
That is it
TVM
Ricardo Conte
If you are not using Page.IspostBack property into your Page_Load then Try to use it
if(!Page.IsPostBack)
{
// Your Code..
}
into your Page_Load.Check MSDN
Hope it works for you.
protected void Page_Load(object sender, EventArgs e)
{
// Your code execute always
if(!Page.IsPostBack)
{
// Code which is execute without postback
}
}

button click event in datagridview

I am having a button cell in datagridview.When that button is clicked,another datagridview should be visible .For every button click in the button column,the data in new datagridview should be differed.I dont know how to implement the button click event which differs for every row.Please help me with the sample code.
You can't implement a button clicked event for button cells in a DataGridViewButtonColumn. Instead, you use the DataGridView's CellClicked event and determine if the event fired for a cell in your DataGridViewButtonColumn. Use the event's DataGridViewCellEventArgs.RowIndex property to find out which row was clicked.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
// Ignore clicks that are not in our
if (e.ColumnIndex == dataGridView1.Columns["MyButtonColumn"].Index && e.RowIndex >= 0) {
Console.WriteLine("Button on row {0} clicked", e.RowIndex);
}
}
The MSDN documentation on the DataGridViewButtonColumn class has a more complete example.
use dataGridView1_CellContentClick instead of dataGridView1_CellClick
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 8) //make sure button index here
{
//write your code here
}
}

asp.net access delete button of gridview

How to access gridview commandfield delete button on RowDataBound event?
How the cells and controls in griview are accessed
Please try the code below. This is for adding a delete confirmation. But you can use it for anything you want.
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].HasControls())
{
LinkButton lnkbtnDelete = ((LinkButton)e.Row.Cells[1].Controls[0]);
lnkbtnDelete.Attributes.Add("onclick", "return confirm('Do you want to Delete?');");
}
}
HTH
See:
protected void YourGrid_RowDataBound(Object sender, GridViewRowEventArgs e)
{
Control button = e.Row.FindControl("btnSubmit");
if (button != null && checkBox is Button)
{
// do what you want
}
}
In RowDataBound event you may access row inner controls through FindControl method.
In the example above I assumed that you control is a Button control with btnSubmit identifier.
Edit: after the author's problem additional explanation:
(ButtonType)e.Row.Cells[commandFieldIndex].Controls[controlIndex];
ButtonType is the type of button being used by the CommandField - Button, LinkButton, or ImageButton. By default, the CommandField uses LinkButtons, but this can be customized via the CommandField’s ButtonType property.

Row Editing in the grid doesn't work in first click

I have a user control which contains a grid and three buttons for add,edit and delete.
I have placed this user control on an asp.net page.
I have OnClick events for these buttons.
When i click on add and delete buttons it's working fine but when i click on edit button,the onclick event of edit button is fired but the row in the grid doesn't appear in the edit mode, i have to click two times.
I don't know where is the problem.The onclick event handler for edit button is as follows:
protected void btnEditBankAccount_Click(object sender, EventArgs e)
{
grdBankAccounts.EditIndex = grdBankAccounts.SelectedIndex;
grdBankAccounts.RowSelectingEnabled = false;
}
Anyone please help.
my user control has a method which binds the grid to the data source, it's as follows
public void SetSupplierData(SupplierType Supplier)
{
if (Supplier != null)
{
ViewState["SupplierID"] = Supplier.SupplierId;
grdBankAccounts.DataSource = Supplier.BankAccounts;
grdBankAccounts.DataBind();
Session["BankAccounts"] = Supplier.BankAccounts;
}
}
the SetSupplierData method is called from the page where i have my user control.
In order to get this "in-place editing" in grids to work, I typically have to data-bind twice:
once in the OnInit or OnLoad method so that the button click event handlers have the data available to work on
in the OnPreRender method again to show the new values / new state (editing or not)
Marc

Resources