ASP.NET Setting a DetailsView BoundField value from javascript - asp.net

I'd like to know if it is possible to set a field of a DetailsView BoundField (in edit mode) from javascript? I can't set an id for the boundfield, so obviously can't use document.getElementById() to get to it, but I was wondering if there's another way to do it?

You could try to create an EditItemTemplate, place a TextBox inside and give it an ID of your choice.
Like this:
<Fields>
<asp:TemplateField HeaderText="Column1">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
If there is stuff to be bound on the TextBox's Text property you could use Bind() method.
Good Luck.

Related

Template Button GridView

I'm using a GridView in my web application and converted the default delete/edit buttons to template fields. I did this because I can't simply delete users from the database with the default SQL delete query that was generated.
I want to use Membership.DeleteUser() however I don't know how to get the username from the GridView that corresponds to the LinkButton that the user presses.
How would I go about doing this?
You can pass value in CommandArgument and get it in the code behind.
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="ibtnDelete"
Text="Delete"
CommandName="Delete"
CommandArgument='<%# Eval("UserName") %>'
</ItemTemplate>
</asp:TemplateField>
You can also use DataKeyNames="UserName" in your gridview and get it in code behind.

ASP GridView DataBind with Entity Navigated property

I have a GridView DataBind with entity ClassA's properties that is working fine.
I am able to directly bind below properties in ASPX file.
ClassA.Id
ClassA.Name
etc.
But ClassA also have a navigation property to related ClassB. I would like in a the same GridView to display related classB's properties.
I try to bind the following in the GridView but it does not work even if I am able to properly evalute the below value in debug mode (entity performs lazy loading when required).
ClassA.classB.Name
How should I proceed ?
You can achive your goal by a template column with an eval function as below;
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"
Text='<%# Eval("ClassA.ClassB.Name") %>'></asp:TextBox>
</EditItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Eval("ClassA.ClassB.Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
The downside of this approach is disabling the two-way databinding feature by using the late-bound eval method.

Multiple databound controls in a single GridView column

I have a grid view that is data bound to a dataset. In a grid I have a column DefaultValue, which has three controls in it - a dropdownlist, a checkbox and a textbox. Depending on the data that is coming in it can switch to any of these controls. Everything is simple enough when we need just to display data - in gridview_prerender event I simply make one of the controls visible. The control is setup like following:
<asp:TemplateField HeaderText="Default Value" SortExpression="DefaultValue">
<ItemTemplate>
<asp:TextBox ID="txt_defaultValue_view" runat="server" Text='<%# Bind("DefaultValue") %>' Enabled ="false" />
<asp:DropDownList ID="ddl_defaultValue_view" runat="server" Enabled ="false" />
<asp:CheckBox ID="chk_defaultValue_view" runat="server" Enabled ="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_defaultValue_edit" runat="server" Text='<%# Bind("DefaultValue") %>'/>
<asp:DropDownList ID="ddl_defaultValue_edit" runat="server" />
<asp:CheckBox ID="chk_defaultValue_edit" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
My problem starts when I am in edit mode and I need to update the grid with new data. Since only the textbox control is databound, the RowUpdating event can only access data from the textbox column and all of my other data simply gets discarded. I also can't databind with checkbox and dropdownlist control, since they can get an invalid data type exception. So, does anyone know how can I update a column that has three different controls, where each of these three controls might have a valid data?
Thanks
First, i would switch visibility of the controls in RowDataBound of the Grid and not on PreRender, because it can only change on DataBinding and not on every postback.
You can there also bind the Dropdown to its datasource, change the checked-state of the Checkbox and set the Text-property of the Textbox according to the the Dataitem of the Row.
When you update you can find your controls with FindControl in RowUpdating and get their values(f.e. Dropdownlist.SelectedValue).
GridView has pair of events:
RowUpdating/RowUpdated
RowDeleting/RowDeleted
....
In your case your need RowUpdating - is called right before update is performed, So find in row expected control (saying checkbox) and make preparation before pass to database
As another way review possibility to use ObjectDataSource event Updating - it is usual way to specify parameters for query execution.

gridview databinder.eval question

We've got a number of gridviews that we're populating with data from an ADABAS datasource via some middleware that, unfortunately, gives us back lots of arrays of stringBuilder that we turn into a datatable, set as the datasource for a gridview, then bind.
It's convoluted, but I'm in a situation where I need to be able to update the gridview (say, drop a row), without going back to the mainframe, deleting the row, and regenerating the datatable, as I would if we had an Oracle datasource.
So, I'm trying to hack something together where on serverside I extract the data from the gridview, build a datatable, delete the given row, then use that datatable as the datasource for the gridview, and rebind.
I can do it on a case by case basis, so long as I know the layout of the gridview. But we're going to be doing this over and over on this current project, I'm trying to figure out a way to abstract it so that I can use it for any gridview I throw at it.
The problem I'm getting is that most of the cols in the gridview are templatefields with asp:labels, where the data is put in with the databinder.eval thing:
<asp:TemplateField HeaderText="Code">
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "code")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblTitle" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "title")%>'></asp:Label></ItemTemplate>
</asp:TemplateField>
So, when building my 'fake' datasource, I need to be able to extract from somewhere in the gridview those column names.
Is that information available in the gridview?
Yikes. You are going to need to store the fake datasource column names within the GridView along with the column values. One suggestion would be to add a HiddenField along with each fake datasource field value. In the example below, display the code (value) in the Label and the column (name) in which code maps in the HiddenField:
<asp:TemplateField HeaderText="Code"
<ItemTemplate>
<asp:Label ID="lblCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "code")%>'></asp:Label>
<asp:HiddenField ID="hfCode" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "codefield")%>'></asp:HiddenField>
</ItemTemplate>
</asp:TemplateField>
It's ugly, but the column names will always be available to you -- no matter what they might be. Best of luck.

Postback a linkbutton in a grid that's under an UpdatePanel

as my title says...
How do you make a LinkButton fire a POSTBACK (not ASYNCPOSTBACK) which is inside a GridView and is under an UpdatePanel?
My Scenario is this,
I have a grid. say table A, which populates a Linkbuttons with link to do Server.Transfer calls from Page1 to Page2.
I have a good reason why i am using a Server.Transfer because of previous page referencing methods and Response.Redirect doesnt fit at all.
normally it would work if i add the grid as a Postback trigger in the UpdatePanel like so
<Triggers><asp:PostBackTrigger ControlID="gvitem" /></Triggers>
but since I have another control inside the grid that needs to be do an AsyncPostback, that would not work also,
all that's lacking is have this line of code, do postback.
<asp:TemplateField HeaderText="Description" SortExpression="ShortDesc">
<ItemTemplate>
<asp:LinkButton ID="btndesc" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ShortDesc")%>' CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Key") %>' />
</ItemTemplate>
</asp:TemplateField>
anyone have an idea?
Find btndesc on grid.ItemDataBound and register it as a PostBackTrigger.

Resources