Here, I have a DropDownList in the EditItemTemplate:
<asp:TemplateField HeaderText="RequestedBy" SortExpression="RequestedBy">
<EditItemTemplate>
<asp:DropDownList ID="ReqUserDDL" runat="server" AppendDataBoundItems ="True" DataSourceID="ReqUsersDataS" DataTextField="Name" DataValueField="Name" SelectedValue='<%# Bind("Name") %>' >
</asp:DropDownList>
<asp:SqlDataSource ID="ReqUsersDataS" runat="server" ConnectionString="<%$ ConnectionStrings:itassetmgmtConnectionString1 %>" SelectCommand="SELECT Firstname + Lastname AS Name FROM Users"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label7" runat="server" Text='<%# Eval("RequestedBy") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
However, I keep getting this error:
DataBinding: 'System.Data.DataRowView' does not contain a property with the name 'Name'.
Is there a way to remedy this?
Use Eval instead of Bind. Bind is for read and/or write, Eval is read-only.
EDIT: Eval/Bind error is not from from the ReqUsersDataS DataSource. It is trying to for a column named 'Name' on the data source of the GridView that has the TemplateField you showed. You just need to make sure that other data source has column named 'Name'.
Related
Is there one gridview event that can access a control in ItemTemplate and EditItemTemplate without additional code (ie. session, viewstate, etc)?
Let's say my gridview looks like this:
<asp:GridView ID="GridView_Sales" runat="server"
AutoGenerateColumns="False"
DataKeyNames="SalesId"
OnRowDataBound="OnRowDataBound"
OnRowEditing="GridView_NSB_RowEditing"
OnRowUpdating="GridView_NSB_RowUpdating"
OnRowCommand="GridView_NSB_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Sold">
<ItemTemplate>
<asp:Label ID="Label_WasSold" runat="server" Text='<%# Eval("WasSold").ToString() %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="DropDownList_Sold" runat="server">
<asp:ListItem Value="Yes"> </asp:ListItem>
<asp:ListItem Value="No"> </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
GridView_RowDataBound has access to Label_WasSold in ItemTempplate but not the dropdown in EditItemTemplate. GridView_RowEditing has access to DropDownList_Sold but not to Label_WasSold; the same thing with GridView_RowUpdating.
I want to compare the value in Label_WasSold.Text to the value in DropDownList_Sold.SelectedValue when doing an update without having to add more code or drag session variables from one place to another.
Just add a hidden field to EditTemplate that stores the value of WasSold data item as in code below.
In your RowUpdating event, you can find the hidden field and get its value, then compare it with drop down value.
Markup to include hidden field in EditTemplate
<asp:TemplateField HeaderText="Sold">
<ItemTemplate>
<asp:Label ID="Label_WasSold" runat="server" Text='<%# Eval("WasSold").ToString() %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:HiddenField id="hdnWasSold" runat="server" Value='<%# Eval("WasSold").ToString() %>' />
<asp:DropDownList ID="DropDownList_Sold" runat="server">
<asp:ListItem Value="Yes"> </asp:ListItem>
<asp:ListItem Value="No"> </asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
C# code to get the hidden field value in RowUpdating event
HiddenField hdnWasSold = (HiddenField)GridView_Sales.Rows[e.RowIndex].FindControl("hdnWasSold");
string wasSoldValue = hdnWasSold.Value;
Here is a code of template field in gridview . i want to get value of this field in string using c# code.
<asp:TemplateField HeaderText="Status" SortExpression="Status">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Status") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server">
<asp:ListItem Value="P"></asp:ListItem>
<asp:ListItem Value="L"></asp:ListItem>
<asp:ListItem Value="A"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
i tried the following code but its not working .
here is my code for reading value.
string str = gridview1.Rows[0].Cells[0].Text.ToString();
but its returning empty string. how can I get selected value.
You can't find the dropdown selected value like this. You need to find the control first using FindControl method:-
DropDownList DropDownList2 = (DropDownList)gridview1.Rows[0].Cells[0]
.FindControl("DropDownList2");
string str = DropDownList2.SelectedValue;
This is my code dropdown list
<asp:TemplateField HeaderText="GroupName">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" Width="100%" Height="10%" DataSourceID="SqlDataSource2" DataTextField="GroupName" DataValueField="GroupName"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:551680A3622135F7C7A89A2044CE6B6E_12 2014\20 12 14\BACKUP 19 NEW\19 NEW\SCHOOLMANAGEMENT\SCHOOLMANAGEMENT\APP_DATA\TCS_SCHOOL.MDFConnectionString %>" SelectCommand="SELECT [GroupName FROM [Group1]"></asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblgroup" runat="server" Text='<%#Eval("GroupName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" Width="100%" Height="10%" DataSourceID="SqlDataSource2" DataTextField="GroupName" DataValueField="GroupName"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:551680A3622135F7C7A89A2044CE6B6E_12 2014\20 12 14\BACKUP 19 NEW\19 NEW\SCHOOLMANAGEMENT\SCHOOLMANAGEMENT\APP_DATA\TCS_SCHOOL.MDFConnectionString %>" SelectCommand="SELECT [GroupName] FROM [Group1]"></asp:SqlDataSource>
</FooterTemplate>
</asp:TemplateField>
I want groupname in datatextfield and groupid in datavaluefield.
How do I do this in C#? How do I bind that two values to dropdownlist in C#??
I see that you have set groupName to both dataText field and dataValue field. Maybe thats the problem? Try to Set
groupId in the dataValue field.
include GroupId in your Select command then set DataValueField="GroupId".
Hope it helps,
Alessandro
I have a gridview to which I dynamically bind data to.
It features SELECT and EDIT commands.
When it is in EDIT mode all the fields turn into textboxes that can be edited.
What I would like to do is add an Ajax Calendar Extender to one of the textboxes.
How can I create a TemplateField just for the one field?
Is it even possible? I know how to do it if I bound all fields and assign the data via a SqlDataSource.
I have tried adding the templatefield to my gridview but the data is just not showing up:
<asp:GridView ID="gvCheckResults" runat="server" OnRowDataBound="gvCheckResults_RowDataBound"
OnRowEditing="gvCheckResults_RowEditing" OnRowUpdating="gvCheckResults_RowUpdating" OnRowCancelingEdit="gvCheckResults_RowCancelingEdit"
OnRowDeleting="gvCheckResults_RowDeleting">
<Columns>
<asp:TemplateField HeaderText="DateOfTest" SortExpression="DateOfTest">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DateOfTest") %>'></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="test" runat="server"
TargetControlID="TextBox1"
CssClass="calendar"
Format="dd/MM/yyyy">
</ajaxToolkit:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("DateOfTest") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
How the data gets bound:
protected void TestDataBind()
{
string Name = txtCheckName.Text;
if (string.IsNullOrEmpty(Name))
Name = null;
gvCheckResults.DataSource = dataContext.GetRecords(Name);
gvCheckResults.DataBind();
}
I have an editable gridview with many columns. Two of which are 100mPlot and SubPlot. SubPlot's are not unique (1A, 1B, 1C, and 1D) but 100mPlot + SubPlot make a unique set of values.
When a user clicks "Edit", they will see a dropdown list in the SubPlot column. This list needs to be based on what the value of 100mPlot is. So in order to display the correct values for SubPlot, I need to pass in a value from 100mPlot column as a parameter to the sqldatasource that would be bound to the SubPlot dropdownlist. How would I go about this?
<asp:BoundField DataField="100mPlot" HeaderText="100m plot"
SortExpression="100mPlot" ReadOnly="True" />
<asp:TemplateField HeaderText="SubPlot" SortExpression="SubPlot">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="dsSubPlotNames"
DataTextField="SiteID" DataValueField="SiteID"
SelectedValue='<%# Bind("SiteID") %>'
AppendDataBoundItems="True">
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsSubPlotNames" runat="server"
ConnectionString="<%$ ConnectionStrings:WERCMTX %>" SelectCommand="exec [339_PPM].usp_SubPlotNames_Select #100mPlotSiteID;">
<SelectParameters>
<asp:Parameter Name="100mPlotSiteID" />
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("SubPlot")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
As you can see, the parameter name "100mPlotSiteID" needs to be taken from 100mPlot column. I'm not sure how else to describe this anymore clearly, let me know if you have any questions. Thanks for your help.
EDIT with newly revised code. So close now!
<asp:TemplateField HeaderText="SubPlot" SortExpression="SubPlot">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="dsSubPlotNames"
DataTextField="SiteName" DataValueField="SiteID"
SelectedValue='<%# Bind("SiteID") %>'
>
</asp:DropDownList>
<asp:SqlDataSource ID="dsSubPlotNames" runat="server"
ConnectionString="<%$ ConnectionStrings:WERCMTX %>" SelectCommand='exec [339_PPM].usp_SubPlotNames_Select null, #100mPlotName;'
CancelSelectOnNullParameter="False">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" DefaultValue='<%# Eval("100mPlot") '
Name="100mPlotName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("SubPlot") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Unfortunately, I get this error with the Eval("100mPlot"):
Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.ControlParameter does not have a DataBinding event.
You need to put a label and set the text property to '<%# Eval("100mPlot") %>' and use a control paremeter in dthe datasource.
If the label's id is "label1" (must be inside edit item template) then use this
<asp:ControlParameter ControlID="label1" PropertyName="Text" Name="100mPlotSiteID" />