FindControl in DetailsView Not working? - c#

I have a Gridview and when the edit button is clicked the details of that row is displayed using a detailsview.
While displaying, I need to find a control in detailsView, and then bind it with a Datasource.
First of All I'm not sure about the event to be used but have used DetailsView1_DataBound. However, if I have to find the control using
var control=(ControlType)DetailsView1.Findcontrol("ID");
Always returns null. May be I am not using the right event, and it couldn't find the control at that point. Any ideas about the event to be used, and the right code please?
Thanks

In your databound event, you need to take care your DetailsView Mode
if (DetailsView1.CurrentMode == DetailsViewMode.Edit)
{
//Put here if you want to find control of your Edit Mode
var control=(ControlType)DetailsView1.Findcontrol("EditTemplateControlID");
}
if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
{
//Put here if you want to find control of your Insert Mode
var control=(ControlType)DetailsView1.Findcontrol("InsertTemplateControlID");
}

I had the same problem and the answer of Muhammad Akhtar did not help me. The problem was solved after changing the default mode of the DetailsView from 'edit' to 'insert'.
I would also recommend the ItemCreated event instead of the DataBound event. I use it often for setting a default value in a field of the DetailsView.

Related

Enable/disable TextBox on FormView.ModeChanged

I have DropDownList and a TextBox in the EditTemplate of my FormView. All I want is to enable/disable the TextBox based on whether the first entry of my DropDownList is selected:
When the FormView is switched to Edit mode by the user
When user changes the selected item of the DropDownList during Edit mode.
I have achieved the second one through JS and that's working fine, but the first one is proving too difficult. I've tried to do this in ModeChanged event of the FormView, but for some reason the following call returns null in the event:
MyFormView.FindControl("MyDropDownListID");
What am I missing here?
(I'm making sure that MyFormView.CurrentMode is FormViewMode.Edit before making the above call)
One of those times when you pull your hair for hours with a problem, then post it on SO and find the solution in the next few minutes. Anyone else trapped into this, the problem is that the controls of a databound FormView aren't created yet at the time of ModeChanged or Page_Load. You must call the above line in DataBound event and it will work fine.

RowDataBound, buttonfield and boundfield

I want to use RowDataBound in order to make sure that the download button(buttonfield) in each row will download the right file according to file name(bondfield) but I have no idea how to make it, since I still new in asp.net and C#. Can anyone give me some idea how to do it?
just bind Command Argument property of your button in grid with bond field.
then on RowDataBound Event just find button and assign downloaded link to it.
or if you want to perform something button click then find your button in RowCommand Event of grid. Assign command name to button.
Visit this link it will help you.
here

Gridview, is there an event, how would I determine that it has finished being rendered?

Gridview, is there an event, how would I determine that it has finished being rendered? That's basically it, I want to adjust the height of some other controls on the page and I want to pick up this event.
Sorry I should have been more explicit in stating that this is actually a web page not a winform. In the end I managed to solve the problem by registering a function with the page load which is called after the controls have been drawn and they calling the gridview size and resizing the other controls to fit. It works and that is that.
Thanks.
The GridView inherits from Control so you can use any of the events, I would try PreRender, it seams it will provide you with what you need
To see all the GridView events available to you;
In Design Mode, select the gridview and click F4.
In the properties window there is a 'lighting' tab which lists all gridview events you can attach to.
Assuming you are in winforms, the best place to do it would be after you bind data to your grid, in the DataBindingComplete event.
Managed to solve it, see details above.

Cant set drop down list selected value in page load

I'm trying to set the selected value of a ddl during page load ie. before databind.
This causes "selected value does not exist" errors. So I force a databind, and add a new element if it does not exist in the data source.
However it looks like when the databind is performed later in the page lifecycle that my added element(s) are removed/overwritten.
Am I setting the values in the wrong part of the life cycle?
what I'm doing seems rather hackish and I think im going about this the wrong way... is there a better way to do this?
Dont do it on PageLoad do it on the DataBound event of the ddl
Did you consider the OnPreRender event of the DDL... I think you will have everything to set the selected value there
However it looks like when the
databind is performed later in the
page lifecycle that my added
element(s) are removed/overwritten.
That is to be expected, databinding clears out the items and rebinds them again. You should look at what points in the page lifecycle you are calling DataBind and also attempting to set the Selected Value.
Have you considered Page_PreRender to set the SelectedValue? This fires after all the initialisation is done, last thing before the page is rendered to the browser. (Hopefully you won't be doing any databinding in Page_PreRender ;))
But it does not seem very logical to be setting the SelectedValue in one place only for it to be overwritten again, you should only be setting the SelectedValue once - after the final .DataBind()
However it looks like when the
databind is performed later in the
page lifecycle that my added
element(s) are removed/overwritten.
As bgs264 says, that's the behaviour of databinding by design. However, if you set the AppendDataBoundItems attribute to true for your DropDownList, this won't happen, your manually added item will remain in place.
<asp:DropDownList runat="server" id="MyDropDownList" AppendDataBoundItems="true" />
My work around to this solution is as follows:
In Page Load:
Page_Load(..)
{
if(...)
{
hidCGroup.value = objCG.CName;
}
}
In DataBound:
ddlContGroup_DataBound(..)
{
ddlContGroup.Items.Insert(0, "--Select--");
ddlContGroup.SelectedIndex = ddlContGroup.Items.IndexOf(ddlContGroup.Items.FindByText(hidCGroup.Value));
}
Now there are two things to take care of. When you are using FindByText and FindByValue always take special care of the value which you are selecting from the ddl.
Sometimes, we use a numeric item as the DataValue and a text item as the DataText, when that happens, you need to interchange FindByText and FindByValue so that the proper selection is made.
Hope this helps.
ddlExample.SelectedIndex=ddlExample.Items.IndexOf(ddlExample.Items.FindByValue(ExampleID.ToString()));
or
ddlExample.SelectedIndex=ddlExample.Items.IndexOf(ddlExample.Items.FindByText(ExampleText.ToString()));

GridView RowDataBound doesn't fire on postback

On an ASP.NET page, I have a GridView populated with the results of a LINQ query. I'm setting the DataSource in code, then calling DataBind on it. In the GridView's RowDataBound event, I'm selectively hiding links in some GridView fields based on the query results. (For instance, I hide the "Show Parent" link of the row in question has no parent row.)
This works fine initially. But on postback (when I don't call DataBind, but the GridView stays populated through ViewState), the data displays, but the RowDataBound event (obviously) doesn't fire, and my links don't get hidden.
What's the best way to get the links to be hidden after a postback?
The RowDataBound event only fires when the GridView's data changes during the postback. The event is short-circuited for speed so it's not re-generating the exact same data unnecessarily. Use the RowCreated event to manipulate the HTML instead - it fires on every postback regardless of whether the data has changed.
Here's how I ended up solving this:
I created a serializable class with readonly properties: PK of a row, and a boolean for each link indicating whether it's enabled or not. We'll call it LinkVisibility.
I created a serializable class inheriting from KeyedCollection to hold instances of the class above.
I created a ViewState-backed property holding an instance of that collection.
In my Search procedure (populating the GridView), I clear the collection.
In RowDataBound, which initially shows/hides the links, I add a LinkVisibility instance to the collection for each row.
In Page.Load, when IsPostBack is true, I loop through the GridView rows. I look up the LinkVisibility for each one by PK in the collection (DataKeyNames is set in the GridView), and I set the links accordingly.
I don't know that this is the best way to do this, but it certainly does work, which is more than I can say for anything else I've tried.
1) You could have a Method - ProcessDataRows() that would get called once on grid_DataBound(...). And then when you need it after PostBack.
And that way you process all rows when you want.
2) You could have methods like ShowParentLink(). That are then bound to the LinkButton in the grid (if you're using an ItemTemplate) and the link would have
Visible='<%#ShowParentLink()%>'
I would have expected the viewstate to also reflect the fact that you have removed some of the links (assuming that they were removed before viewstate was saved).
Maybe thats the question you need to ask 'why do the removed links still appear in viewstate?'.
Another solution is to put the logic in the LINQ query, so that you end up with a boolean LINQ field like "ShowParentLink". Then you can just bind the Visible property of the HyperLink field to that value - no RowDataBound required.
protected void btnHazardRating_Click(object sender, EventArgs e)
{
gvPanelRole.RowDataBound += new GridViewRowEventHandler(gvPanelRole_RowDataBound);
gvPanelRole.DataSource = dtGo;
gvPanelRole.DataBind();
ModalPopup.Show();
}
void Process Rows()
{
... do something
... process complete
datagrid.DataBind();
}
A page cannot process postback events unless it is rebuilt exactly as it was before (the postback). If you re-hide your links during the page-init, then your click events and such should fire. Unfortunately, without seeing some sample code I can't get more specific.
Also the data RowDataBound does not fire because you are not data binding. You are rebuilding the page from the viewstate- "viewstate binding" for lack of a better word.

Categories

Resources