I have a requirement to append data to the DataNavigateUrlFormatString property of the HyperLinkField of a GridView control.
My problem is I can't seem to find any way to acquire a handle to the control in the RowDataBound event of the Grid. Unlike other controls in the Grid, it doesn't seem to be accessible in the same way b/c it doesn't have a ControlID to find it. So where this will work with other controls
CheckBox status = (CheckBox)e.Row.FindControl("chkStatus");
if (status != null)
{
// do something here
}
The same won't for the Hyperlinkfield because I can't use the FindControl method.
I know there has to be some way to access it b/c inspecting the e.Row object I can clearly see it and the DataNavigateUrlFormatString property I'd like to modify. I just haven't been able to find a way to acquire a handle to it.
Every post I've found makes reference to the HyperLink control, not the HyperLinkField. I prefer not changing the existing Grid structure of this item to a HyperLink if I can avoid it.
How can you access the HyperLinkField in the RowDataBoundevent? Is it possible?
Related
I have two pages on which I bound dynamic controls . All the controls what I bound are TextBoxes. Whenever I tried to retrieve the values of the controls in first page using
Page.Findcontrol("ctl00$cph_cabFest$AccountNumber")
It gives the proper result. But the second page gives the result as NULL. What could be the exact issue? Could anyone suggest me some way to identify this?
Just take a look at your event cycle. The best way to overcome these kind of issues is to bind the controls in Page_Init as well.
There might be another reason for this. Bind your Panel in each PostBack request.
I am a facing a situation in which I need to use something like a ButtonList inside a DataList control.
Since there is no control like ButtonList, should I nest a DataList/Repeater inside the DataList or there is some other better option to handle the situation.
I am not exactly sure what you are looking for but this is what I assume you want:
YourListItem1
ButtonAction1
ButtonAction2
ButtonAction3
ButtonAction4
YourListItem2
ButtonAction1
ButtonAction3
YourListItem3
ButtonAction1
ButtonAction2
YourListItem3
ButtonAction3
ButtonAction4
Or something similar?
To produce this you could just have a DataList with a Repeater inside that contains the buttons you need. You could implement the OnDataBinding event of your DataList (YourListItem) and then bind the data that produces the buttons based on some data. Then in the Repeater you could implement each button's OnDataBinding event and assign the CommandArguments with the ID or detail you need to make the button act specific to the row it is on.
This method would allow you to make one function for each button used in your template and the CommandArgument would define the details of the action.
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.
I have created my own control which contains TextBox control as a child control. this textbox has autopostback set on. when the postback is called I dont get any text in request.querrystring.
where should I specify text entered into texbox will be send to response ? or is there any easier way of sending this information? I want to have the entered text once again in textbox after postback.
thank you
For dynamically created controls, you need to create them and add them to the control tree in the Page_Init() event; Page_Load() is too late, and Page_PreRender() is way too late.
Once in the control tree, the runtime will restore the state of the control for you after a postback. You can access the contents of the control using myTextBox.Text -- you don't need to use Request.QueryString
For a control that only contains a TextBox, things would be much easier if you used a user control rather than an INamingContainer. That way, you could just use <asp:TextBox> in your .ascx markup.
A custom control? Any posted control like this loads its data in LoadPostData method. You can override this in your custom control class, and access the value from teh control via:
var text = postDataCollection[textboxID];
The textbox within your custom control has to have an ID.
If you are talking something else, let me know the specifics.
Thanks.
(TextBox)Control.FindControl("control id")
get dynamic control's value in postback , Request.Form("control client id")
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.