Changing the FormView record while in insert mode - c#

I have 2 ObjectDataSource's - 1 for the GridView and 1 for the FormView.
I enabled selection on the GridView and all is well; the page loads, you select a record, and the FormView populates with the record.
However, once the FormView is in Insert mode, when I select a record on the GridView, it is not changing back to display the record I selected.
Where could I handle this in the code, as I don't see how to access the "Enable Selection" on the GridView in code behind?
What I have tried
protected void grdSearchGroup_SelectedIndexChanged(object sender, EventArgs e)
{
formGroupInput.DefaultMode = FormViewMode.ReadOnly;
formGroupInput.DataBind();
}
protected void grdSearchGroup_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
formGroupInput.DefaultMode = FormViewMode.ReadOnly;
formGroupInput.DataBind();
}

I think the simplest way to solve your problem is to handle the GridView's SelectedIndexChanging method, and set the FormView back to read-only mode (that way you can see the records details in the FormView like you want).
protected void yourGridView_SelectedIndexChanging(Object sender, EventArgs e)
{
yourFormView.ChangeMode(FormViewMode.ReadOnly);
}
Note that you must call the FormView's ChangeMode method in order to update the mode. Just setting the DefaultMode property is not enough.

Related

Cannot change button properties in C# Webform GridView _RowEditing event

I have a GridView object on a C# WebForm page which displays table data and allows editing. I have made a Template Button column to execute the Update Command in my SQLDataSource Object.
The buttons are enabled by default, which caused errors when they were pressed without the Row being in Edit Mode. So I used the GridView1_DataBound() Event to disable the buttons for each row using FindControl on the appropriate column. This works fine.
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gvrow in GridView1.Rows)
{
Button setButton = (Button)gvrow.Cells[7].FindControl("Button1");
setButton.Visible = False;
}
}
What I want to do is when a row is selected for editing, I enable only that row's button. I have been able to use FindControl to get a reference to the button (verified I have the reference doing Debug) But when I make changes to the button, it doesn't work.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
DataBind();
int c = GridView1.Rows[e.NewEditIndex].Cells.Count - 1;
Button setButton = (Button) GridView1.Rows[e.NewEditIndex].Cells[c].FindControl("Button1");
// doesn't work!!
setButton.Visible = true;
}
In debug mode, I verified that I do have access to the button for the row and can see its properties, etc.
I have tried changing forecolor, text, etc. but nothing changes on the actual button; what do I need to do in order to change the button?

How to get asp.net dropdownlist to allow selections

I have a dropdownlist on a webform that I fill from a sql query, I then want to be able to select individual items in the dropdown and have corresponding fields from a datatable fill textboxes on the form
Problem is rowSel is returning 0 and the dropdown won't let me select any other item it always snaps back to the first itenm in the list.
Thought this might have something to do with autopostback being set to true, but if I set it to false that causes otheer problems, Not sure what else to try Im a winforms person and very new to asp.net
protected void ddClients_SelectedIndexChanged(object sender, EventArgs e)
{
int rowSel = ddClients.SelectedIndex;
txtClient.Text = dsShow.Rows[rowSel["ClientsTableFieldA"].ToString();
}
It should allow me to select a value from the drop down then populate some textboxes with fields from the datatable.
You could try:
protected void ddClients_SelectedIndexChanged(object sender, EventArgs e)
{
txtClient.Text = ddClients.SelectedItem.Value.ToString();
}
protected void ddClients_SelectedIndexChanged(object sender, EventArgs e)
{
txtClient.Text = ddClients.SelectedItem.Text;
}
As suggested by B. Seberle
DDL items have fields value and text so it depends how you bound the ddl to SQL datasource.
place break point on txtClient.Text = ddClients.SelectedItem.Text see if item list is empty.
it should not be necessary but you can force a ddClient.databind() in page_load if(!Page.IsPostback).
however ddClients_SelectedIndexChanged will only trigger on postback.

why my radcombobox is empty after postback?

I have a radcombobox,I want get checked items and save it in database but when i click save button,page is load again and my radcombobox become empty and then all of my checked items disappear.please help me,how can keep ckeckeditems?
As D Stanley mentioned in the comments, you're probably not checking for a postback when populating your dropdown.
This is the general approach you need to use in your code...
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateTheDropdown();
}
}
private void PopulateTheDropdown()
{
// Populate / databind your dropdown here
}
This will ensure your dropdown is not rebound when a postback occurs so you don't lose the selected value(s).
If you have autopostback activated you must save the selected value separately. Try to explicitly disable and check the value when the event fires:
<telerik:RadComboBox ID="RadComboBoxControl" AutoPostBack="false" OnSelectedIndexChanged="RadComboBoxControl_SelectedIndexChanged" runat="server" EmptyMessage="Select something"></telerik:RadComboBox>
protected void RadComboBoxControl_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
// Only test
var seleccionado = RadComboBoxControl.Items.FindItemByText(e.Text);
}
Check if you have assigned DataSource of the control in other parts of the code
RadComboBoxControl.DataSource = ...
RadComboBoxControl.DataBind();
This will also lose the selected element

Get data from a specific column when pressing edit on that row in GridView

How do I get data from a specific column that the edit button was pressed on in a gridview?
The following code doesnt work:
protected void viewStoryTime_OnRowEditing(object sender, GridViewEditEventArgs e)
{
SqlDataSource10.UpdateParameters["setEditHoursParam"].DefaultValue = viewStoryTime.SelectedRow.Cells[0].Text;
}
This is because the row is not actually selected. How can I accomplish this?
e.NewEditIndex has the row index of the currently-editing row. You can use that to access the row and read the cell data as necessary.
The parameter GridViewEditEventArgs contains the row index for the current edited row from the gridview.
You should do something like this
SqlDataSource10.UpdateParameters["setEditHoursParam"].DefaultValue = MyGridView.Rows[e.NewEditIndex].Cells[0].Text;
Another way could be implement a RowCommand event where parameter GridViewCommandEventArgs carries the command name, then you should do something like this:
void MyGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="my command name")
{
//Do stuff here
}
}

Gridview PageIndexChanging not Firing

I am writing my own event handler for GridView in PageIndexChanging event, I didn't explicit set the DataSourceID for my GridView, here is my code:
Code for GridView data binding:
protected void DetailsView_DataBound (object sender, EventArgs e )
{
Customer cust = (Customer)DetailsView.DataItem;
this.GridView.DataSource = cust.Orders;
this.GridView.DataBind();
}
This part of the code allows me to show order details in GridView when data bound with DetailsView. Then I write my own GridView_PageIndexChanging event handler and DOES NOT work for me:
protected void GridView_PageIndexChanging(object sender, EventArgs e)
{
GridView.PageIndex = e.NewPageIndex();
GridView.DataBind();
}
If I click the next page number, the website shows nothing. But if I change GridView.DataBind() to DataBind() The paging works.
Anyone has any idea why the second Databind method works and what is the reason?
the second databind is DataBind(Page.DataBind) which refrence to current page binds all page controls and its child controls.
Have you set allowPaging="True" of gridview. if not then set it to true.

Categories