I want to set a value (Field IDCommerciale) when I click the insert button on a details view. I tried to change the value using OnItemInserting but nothing happens.
This is the code on the aspx:
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" DefaultMode="Insert"
AutoGenerateRows="False" DataKeyNames="IDPianificazione"
DataSourceID="EntityDataSourceDetailsView" OnItemInserting="insertingmodeevent"
>
<Fields>
<asp:TemplateField HeaderText="IDCommerciale"
SortExpression="IDCommerciale">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Commerciali.Nome") %>'></asp:Label>
</ItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="ddlIDCommerciale" runat="server" SelectedValue='<%# Bind("IDCommerciale") %>'
DataSourceID="EntityDataSource1" DataTextField="Nome"
DataValueField='IDCommerciale'>
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="IDCLiente" SortExpression="IDCLiente">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("IDCLiente") %>'></asp:Label>
</ItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("IDCLiente") %>'></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="IDCategoria" SortExpression="IDCategoria">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("IDCategoria") %>'></asp:Label>
</ItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("IDCategoria") %>'></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Oggetto" HeaderText="Oggetto"
SortExpression="Oggetto" />
<asp:BoundField DataField="datainizio" HeaderText="datainizio"
SortExpression="datainizio" />
<asp:BoundField DataField="DataPresuntaFine" HeaderText="DataPresuntaFine"
SortExpression="DataPresuntaFine" />
<asp:BoundField DataField="DataCompletamento" HeaderText="DataCompletamento"
SortExpression="DataCompletamento" />
<asp:CommandField
ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
<asp:EntityDataSource ID="EntityDataSourceDetailsView" runat="server"
ConnectionString="name=SalesPortalEntities" Include="Categorie,Commerciali,Clienti"
DefaultContainerName="SalesPortalEntities" EnableDelete="True"
EnableFlattening="False" EnableInsert="True" EnableUpdate="True"
EntitySetName="PianificazioneIncontri">
</asp:EntityDataSource>
<asp:EntityDataSource ID="EntityDataSource1" runat="server"
ConnectionString="name=SalesPortalEntities"
DefaultContainerName="SalesPortalEntities" EnableFlattening="False"
EntitySetName="Commerciali">
</asp:EntityDataSource>
and this is the code behind:
protected void insertingmodeevent(object sender, EventArgs e)
{
string gruppoAdmin = System.Web.Security.FormsAuthentication.Decrypt(Request.Cookies[1].Value).UserData;
if (gruppoAdmin.Contains("SPGAdmins"))
{
try
{
DropDownList dd = new DropDownList();
dd = DetailsView1.FindControl("ddlIDCommerciale") as DropDownList;
dd.SelectedIndex = 1;
dd.SelectedValue = "1";
}
catch (Exception)
{
new Exception();
}
}
else
{
// other code
}
}
The code is execude regularly but if I check the data on the Database, the Item does'ent take the value forced on the code behind. I tried also with The event OnDataBinding but the result is the same.
How can I force the value for this field?
A possible solution is to add an event OnItemInserting on the entitydatasource of my gridview. Then I used this code:
protected void UpdateDataSource(object sender, EntityDataSourceChangingEventArgs e)
{
if (!gruppoAdmin.Contains("SPGAdmins"))
{
SalesPortalModel.PianificazioneIncontri pianificazione = e.Entity as SalesPortalModel.PianificazioneIncontri;
int idCommerciale = (from a in entity.Commerciali where a.Nome == Context.User.Identity.Name select a.IDCommerciale).First();
pianificazione.IDCommerciale = idCommerciale;
detailsView1.Rows[1].Visible = false;
}
}
Related
I use a CalendarExtender of AjaxControlToolkit in a GridView and a TextBox. And from that TextBox I want to disable future dates.
I am trying below code
<asp:GridView ID="Gridview2" runat="server" AutoGenerateColumns="false" DataKeyNames="Sno" ShowFooter="true" OnRowCommand="Gridview2_RowCommand" CssClass="table table-bordered table-striped"
OnRowCancelingEdit="Gridview2_RowCancelingEdit" OnRowDeleting="Gridview2_RowDeleting" OnRowEditing="Gridview2_RowEditing" OnRowUpdating="Gridview2_RowUpdating" OnRowDataBound="Gridview2_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Sno" Visible="false">
<ItemTemplate>
<asp:Label ID="lbl_sno1" runat="server" Text='<%#Eval("Sno") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date of visit" ShowHeader="false">
<EditItemTemplate>
<asp:TextBox ID="Txt_update_DateofVisit1" runat="server" Text='<%# Eval("DateofVisit","{0:MM/dd/yyyy}") %>' CssClass="form-control"/>
<cc1:CalendarExtender ID="CalendarExtenderedit11" CssClass="Calendar" TargetControlID="Txt_update_DateofVisit1" Format="MM/dd/yyyy" runat="server"></cc1:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbl_DateofVisit1" runat="server" Text='<%# Eval("DateofVisit","{0:MM/dd/yyyy}") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt_DateofVisit_add" runat="server" CssClass="form-control" />
<cc1:CalendarExtender ID="CalendarExtendere21" CssClass="Calendar" TargetControlID="txt_DateofVisit_add" Format="MM/dd/yyyy" runat="server"></cc1:CalendarExtender>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="imgbtnDelete_add" runat="server" CommandName="Delete" ImageUrl="~/img/grid_delete.png" Text="Edit" ToolTip="click here to delete" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="imgbtnAdd_add" runat="server" CommandName="AddNew" ImageUrl="~/img/grid_add.png" ToolTip="click here to add" CausesValidation="true" ValidationGroup="outadd" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Below is my RowDataBound
protected void Gridview2_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
AjaxControlToolkit.CalendarExtender CalendarExtendere21 = (AjaxControlToolkit.CalendarExtender)Gridview2.FooterRow.FindControl("CalendarExtendere21");
CalendarExtendere21.EndDate = DateTime.Now;
// CalendarExtendere21.StartDate = DateTime.Now;
}
}
I get below error:
Object reference not set to an instance of an object.
just use parameter e to find out control in grid
if (e.Row.RowType == DataControlRowType.Footer)
{
AjaxControlToolkit.CalendarExtender CalendarExtendere21 =
(e.Row.FindControl("CalendarExtendere21") as AjaxControlToolkit.CalendarExtender);
}
and don't forget to check for null
I have grid with text box for filter data on header.I have button outside the grid.I am using this gridview for filtering the grid by some columns. When I click on button I want to get value from text box and dropdownlist on codebehind.cs, below is my webform.aspx code of gridview,
<asp:Button ID="btngetLocationDate" runat="server" Text="Get Filtered Data" OnClick="getTextBoxValue"></asp:Button>
<asp:GridView ID="TrackerGrid" runat="server" BackColor="#CCCCCC" AllowPaging="True" AutoGenerateColumns="false">
<AlternatingRowStyle HorizontalAlign="Center" VerticalAlign="Middle" />
<Columns>
<asp:TemplateField HeaderText="User ID" SortExpression="User ID">
<ItemTemplate>
<asp:Label ID="lbluserid" runat="server" Text='<%# Eval("User ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Customer Name">
<HeaderTemplate>
Full Name:
<asp:DropDownList ID="ddlfullName" runat="server" AutoPostBack="true" OnSelectedIndexChanged="onselectDropdown">
<asp:ListItem Text="ALL" Value="ALL" Selected="True"></asp:ListItem>
</asp:DropDownList>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# Eval("Full Name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Mobile Number">
<HeaderTemplate>
Mobile Number:
<asp:TextBox ID="txtMobilenumber" runat="server" AutoPostBack="true"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblmobile" runat="server" Text='<%# Eval("Mobile Phone") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email Address">
<HeaderTemplate>
Email Id:
<asp:TextBox ID="txtemail" runat="server" AutoPostBack="true"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblemail" runat="server" Text='<%# Eval("Email ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Longitude">
<HeaderTemplate>
<asp:Label ID="lbllng" runat="server" Text="Longitude"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbllongi" runat="server" Text='<%# Eval("Longitude") %>' Width="70"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Lattitude">
<HeaderTemplate>
<asp:Label ID="lbllat" runat="server" Text="Lattitude"></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbllatti" runat="server" Text='<%# Eval("Lattitude") %>' Width="70"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Activated On">
<HeaderTemplate>
<asp:Label ID="lbldevices" runat="server" Text="Activated On"></asp:Label></br>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbldevice" runat="server" Text='<%# Eval("Activated On") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location DateTime" SortExpression="Location DateTime">
<HeaderTemplate>
<asp:Label ID="Label1" runat="server" Text="Location DateTime"></asp:Label></br>
<asp:Label ID="lblStartDate" runat="server" AssociatedControlID="txtStartDate" Text="Start Date"></asp:Label>
<asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
<cc1:calendarextender id="CalendarStartDate" runat="server" format="dd/MM/yyyy" targetcontrolid="txtStartDate" popupbuttonid="imgPopup1"></cc1:calendarextender>
<asp:ImageButton ID="imgPopup1" runat="server" ImageUrl="https://cdn4.iconfinder.com/data/icons/small-n-flat/24/calendar-alt-512.png" Height="20px" Width="30px" />
</br>
<asp:Label ID="lblEndDate" runat="server" Text="End Date" AssociatedControlID="txtEndDate"></asp:Label>
<asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox>
<cc1:calendarextender id="CalendarEndDate" runat="server" format="dd/MM/yyyy" targetcontrolid="txtEndDate" popupbuttonid="imgPopup2"></cc1:calendarextender>
<asp:ImageButton ID="imgPopup2" runat="server" ImageUrl="https://cdn4.iconfinder.com/data/icons/small-n-flat/24/calendar-alt-512.png" Height="20px" Width="30px" />
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblDatetime" runat="server" Text='<%# Eval("Location DateTime") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and I am getting texbox value in codebehind code.
protected void getTextBoxValue(object sender, EventArgs e)
{
DropDownList ddlfullname = (TrackerGrid.HeaderRow.FindControl("ddlfullName") as DropDownList);
string selectedValue = ddlfullname.SelectedItem.Text;
TextBox mobilenumber = (TrackerGrid.HeaderRow.FindControl("txtMobilenumber") as TextBox);
string mobile = mobilenumber.Text;
TextBox email = TrackerGrid.HeaderRow.FindControl("txtemail") as TextBox;
string emailid = email.Text;
TextBox staloc = TrackerGrid.HeaderRow.FindControl("txtStartDate") as TextBox;
string startlocatdate = staloc.Text;
TextBox endloc = TrackerGrid.HeaderRow.FindControl("txtEndDate") as TextBox;
string endlocadate = endloc.Text;
}
I am getting empty value when I debugging this code.I think my code is correct but how i am not getting value of textboxes, i don't understand. Please help me for solving my issue.
Thanks in advance.
You probably did not wrap the DataBind() of the Gridview inside an IspostBack check. If you do not then the values will be rested by the DataBind instead of being saved with ViewState.
protected void Page_Load(object sender, EventArgs e)
{
//not here
TrackerGrid.DataSource = source;
TrackerGrid.DataBind();
if (IsPostBack == false)
{
//but inside an ispostback check
TrackerGrid.DataSource = source;
TrackerGrid.DataBind();
}
}
i found many article and i make this code
i have this error
Unable to cast object of type 'AllProjects_Result' to type 'Project'.
<asp:GridView ID="GVAllProjects" runat="server"
OnRowDataBound="GVAllProjects_RowDataBound"
OnRowDeleting="GVAllProjects_RowDeleting"
OnRowEditing="GVAllProjects_RowEditing"
OnRowUpdating="GVAllProjects_RowUpdating"
AutoGenerateColumns="False" AllowPaging="True" PageSize="5" DataKeyNames="ID">
<Columns>
<asp:CommandField ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" ItemStyle-Width="150" />
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lblProID" Visible="false" runat="server" Text='<%# Eval("ID")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="المشروعات" ItemStyle-Width="150" >
<ItemTemplate>
<asp:Label ID="lblProjectName" runat="server" Text='<%# Eval("ProjectName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtProjectName" runat="server" Text='<%# Eval("ProjectName") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="الموقع" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblDistrictName" runat="server" Text='<%# Eval("DistrictName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlDistrictName" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valDistrictName" runat="server" ControlToValidate="ddlDistrictName"
Display="Dynamic" ErrorMessage="DistrictName is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="editGrp">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlDistrictNameNew" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valDistrictNameNew" runat="server" ControlToValidate="ddlDistrictNameNew"
Display="Dynamic" ErrorMessage="DistrictName is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="newGrp">*</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="المساحات" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblAreas" runat="server" Text='<%# Eval("Areas")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtAreas" runat="server" Text='<%# Eval("Areas") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="نظام السداد" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblPaymentSystem" runat="server" Text='<%# Eval("PaymentSystem")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPaymentSystem" runat="server" Text='<%# Eval("PaymentSystem") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="تاريخ الاستلام" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblReceivedDate" runat="server" Text='<%# Eval("ReceivedDate", "{0:d}")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEditDate" runat="server"></asp:TextBox>
<asp:ImageButton runat="server" ID="imgPopup" ImageUrl="~/images/-calendar.png" ImageAlign="Bottom" />
<ajaxToolkit:calendarextender popupbuttonid="imgPopup" id="CalendarExtender1" runat="server" targetcontrolid="txtEditDate"
format="MM/dd/yyyy" enabled="true" />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtAddDate" runat="server"></asp:TextBox>
<asp:ImageButton runat="server" ID="imgPopup" ImageUrl="~/images/-calendar.png" ImageAlign="Bottom" />
<ajaxToolkit:calendarextender id="CalendarExtender2" runat="server"
targetcontrolid="txtAddDate" popupbuttonid="imgPopup" enabled="true" format="dd/MM/yyyy" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="تصنيف العقار" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblClassification" runat="server" Text='<%# Eval("PropertyClassification")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtClassification" runat="server" Text='<%# Eval("PropertyClassification") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="صورة المشروع" ItemStyle-Width="150">
<ItemTemplate>
<img src='ProjectsImages/<%# Eval("ProjectImage") %>' style="width:120px;height:120px;" />
</ItemTemplate>
<EditItemTemplate>
<asp:FileUpload ID="UploadPhoto" runat="server" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="الغرض" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblPurposeName" runat="server" Text='<%# Bind("PurposeName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlPurposeName" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valPurposeName" runat="server" ControlToValidate="ddlPurposeName"
Display="Dynamic" ErrorMessage="PurposeName is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="editGrp">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlPurposeNameNew" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valClassificationNew" runat="server" ControlToValidate="ddlPurposeNameNew"
Display="Dynamic" ErrorMessage="DistrictName is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="newGrp">*</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="النوع" ItemStyle-Width="150">
<ItemTemplate>
<asp:Label ID="lblTypes" runat="server" Text='<%# Bind("TypeName")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlTypes" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valTypes" runat="server" ControlToValidate="ddlTypes"
Display="Dynamic" ErrorMessage="Types is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="editGrp">*</asp:RequiredFieldValidator>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlTypesNew" runat="server">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="valTypesNew" runat="server" ControlToValidate="ddlTypesNew"
Display="Dynamic" ErrorMessage="Types is required." ForeColor="Red" SetFocusOnError="True"
ValidationGroup="newGrp">*</asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and this is my code :
protected void GVAllProjects_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList ddlDistrictName = null;
DropDownList ddlPurposeName = null;
DropDownList ddlTypes = null;
if (e.Row.RowType == DataControlRowType.Footer)
{
ddlDistrictName = e.Row.FindControl("ddlDistrictNameNew") as DropDownList;
ddlPurposeName = e.Row.FindControl("ddlPurposeNameNew") as DropDownList;
ddlTypes = e.Row.FindControl("ddlTypesNew") as DropDownList;
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
ddlDistrictName = e.Row.FindControl("ddlDistrictName") as DropDownList;
ddlPurposeName = e.Row.FindControl("ddlPurposeName") as DropDownList;
ddlTypes = e.Row.FindControl("ddlTypes") as DropDownList;
}
if (ddlDistrictName != null)
{
using (AlamaarRealEstateEntities context = new AlamaarRealEstateEntities())
{
ddlDistrictName.DataSource = context.Districts.ToList();
ddlDistrictName.DataTextField = "DistrictName";
ddlDistrictName.DataValueField = "ID";
ddlDistrictName.DataBind();
ddlPurposeName.Items.Insert(0, new ListItem(""));
ddlPurposeName.DataSource = context.Purposes.ToList();
ddlPurposeName.DataTextField = "PurposeName";
ddlPurposeName.DataValueField = "ID";
ddlPurposeName.DataBind();
ddlTypes.Items.Insert(0, new ListItem(""));
ddlTypes.DataSource = context.Types.ToList();
ddlTypes.DataTextField = "TypeName";
ddlTypes.DataValueField = "ID";
ddlTypes.DataBind();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
ddlDistrictName.SelectedValue = ((Project)(e.Row.DataItem)).DistrictID.ToString();
ddlPurposeName.SelectedValue = ((Project)(e.Row.DataItem)).PurposeID.ToString(); ;
ddlTypes.SelectedValue = ((Project)(e.Row.DataItem)).TypeID.ToString();
}
}
}
}
void BindGrid()
{
using (AlamaarRealEstateEntities context = new AlamaarRealEstateEntities())
{
if (context.Projects.Count() > 0)
{
GVAllProjects.DataSource = context.AllProjects().ToList();
GVAllProjects.DataBind();
}
else
{
var obj = new List<Project>();
obj.Add(new Project());
// Bind the DataTable which contain a blank row to the GridView
GVAllProjects.DataSource = obj;
GVAllProjects.DataBind();
int columnsCount = GVAllProjects.Columns.Count;
GVAllProjects.Rows[0].Cells.Clear();// clear all the cells in the row
GVAllProjects.Rows[0].Cells.Add(new TableCell()); //add a new blank cell
GVAllProjects.Rows[0].Cells[0].ColumnSpan = columnsCount; //set the column span to the new added cell
//You can set the styles here
GVAllProjects.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
GVAllProjects.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red;
GVAllProjects.Rows[0].Cells[0].Font.Bold = true;
//set No Results found to the new added cell
GVAllProjects.Rows[0].Cells[0].Text = "NO RESULT FOUND!";
}
}
}
the error here in this lines :
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
ddlDistrictName.SelectedValue = ((Project)(e.Row.DataItem)).DistrictID.ToString();
ddlPurposeName.SelectedValue = ((Project)(e.Row.DataItem)).PurposeID.ToString(); ;
ddlTypes.SelectedValue = ((Project)(e.Row.DataItem)).TypeID.ToString();
}
}
any help is appreciated.
Here is my code where it's giving the value of ddl = null:
//event handler to bind the data to the row before rendering the gridview
protected void gridview1_RowDataBound(object sender ,GridViewRowEventArgs e)
{
if ((e.Row.RowType == DataControlRowType.DataRow)&&(e.Row.DataItem!=null))
{
//DropDownList ddlNo = (DropDownList)e.Row.FindControl("ddlNo");
DropDownList ddlNo = (DropDownList)e.Row.FindControl("ddlNo");
if (ddlNo != null)
{
ddlNo.DataSource = Test.GetData();
ddlNo.DataBind();
ddlNo.SelectedValue = gridview1.DataKeys[e.Row.RowIndex].Values[0].ToString();//datakeys are used to retain the value of the no. in edit mode
}
}
}
Here is my aspx code:
<asp:GridView ID="gridview1" runat="server" AutoGenerateColumns="false" AutoGenerateDeleteButton="false" AutoGenerateEditButton="false" ShowFooter="true" EmptyDataText="No data available." PagerStyle-HorizontalAlign="Right" AllowPaging="true" AllowSorting="true" GridLines="Vertical" Width="98%" EnableViewState="true" OnRowCancelingEdit="gridview1_RowCancelingEdit" OnRowDataBound="gridview1_RowDataBound" OnRowEditing="gridview1_RowEditing" OnRowUpdating="gridview1_RowUpdating" OnRowCommand="gridview1_RowCommand" OnRowDeleting="gridview1_RowDeleting" DataKeyNames="testID">
<Columns>
<asp:TemplateField HeaderText="Select" HeaderStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="lblSelect" runat="server" Text="Select"></asp:Label>
<%-- <asp:CheckBox ID="chkSelect" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckChanged" />--%>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:BoundField DataField="testID" HeaderText="SearchId" />
<asp:TemplateField HeaderText="Name" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%#Eval("testDesc") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewName" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Bind("testDesc") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="No." HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:DropDownList ID="ddlNo" runat="server" DataTextField="testNo" DataValueField="testNo" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblNo" runat="server" Text='<%# Bind("testNo") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlNewNo" runat="server" DataTextField="testNo" DataValueField="testNo">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="linkbt1" runat="server" CausesValidation="true" CommandName="Update" Text="Update">
</asp:LinkButton>
<asp:LinkButton ID="linkbt2" runat="server" CausesValidation="false" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="linkbt1" runat="server" CausesValidation="false" CommandName="Edit" Text="Edit">
</asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="linkbt2" runat="server" CausesValidation="false" CommandName="AddNew" Text="AddNew">
</asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="true" ShowHeader="true" />
</Columns>
</asp:GridView>
Can anyone provide a relevant solution to this?
you put the ddl no in the footer template but you're only checking for that in the (e.Row.RowType == DataControlRowType.DataRow) Change that to FooterRow and it should find it
change the above line to
if(e.Row.RowType == DataControlRowType.Footer && e.Row.DataItem != null)
<%--DataKeyNames="Environment_ID"--%>
' />
<%--
'>
' />
--%>
' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblapp" runat="server" Text='<%# Eval("AppName") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlAppFooter" class="SearchBoxRTD" runat="server" DataTextField="AppName"
DataValueField="SNO">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="TestType" HeaderStyle-Width="50">
<EditItemTemplate>
<asp:DropDownList ID="ddlTestTypeDataRow" class="SearchBoxRTD" runat="server" DataTextField="Testingtypedescription"
DataValueField="TestingTypeID">
</asp:DropDownList>
<asp:HiddenField ID="hdnTesttype" runat="server" Value='<%# Bind("Testtype") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblTestType" runat="server" Text='<%# Eval("Testingtypedescription") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlTestTypeFooter" class="SearchBoxRTD" runat="server" DataTextField="Testingtypedescription"
DataValueField="TestingTypeID">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Scenario Type" HeaderStyle-Width="50">
<EditItemTemplate>
<asp:DropDownList ID="ddlScenarioTypeDataRow" class="SearchBoxRTD" runat="server" DataTextField="ScenarioTypedescription"
DataValueField="ScenarioTypeID">
</asp:DropDownList>
<asp:HiddenField ID="hdnScenariotype" runat="server" Value='<%# Bind("ScenarioTypeID") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblScenarioType" runat="server" Text='<%# Eval("ScenarioTypedescription") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlScenarioTypeFooter" class="SearchBoxRTD" runat="server"
DataTextField="ScenarioTypedescription" DataValueField="ScenarioTypeID">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Scenario">
<EditItemTemplate>
<asp:TextBox ID="txtScenario" runat="server" Text='<%# Bind("Scenario") %>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RFDGEdUI" runat="server" Font-Bold="true" ErrorMessage="*"
ControlToValidate="txtScenario" ValidationGroup="EditScenario"></asp:RequiredFieldValidator>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblScenario" runat="server" Text='<%# Bind("Scenario") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtfootScenario" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RFDGScenario" runat="server" Font-Bold="true" ErrorMessage="*"
ControlToValidate="txtfootScenario" ValidationGroup="AddScenario"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status">
<EditItemTemplate>
<asp:CheckBox ID="chkScenariostatus" runat="server" Checked='<%# Bind("Status") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LblScenarioStatus" runat="server" Text='<%# Bind("Status") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:CheckBox ID="chkScenariofooter" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="lbkUpdateScenario" runat="server" ValidationGroup="EditScenario"
CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
<asp:LinkButton ID="lnkCancelScenario" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkAddScenario" runat="server" ValidationGroup="AddScenario"
CausesValidation="True" CommandName="Insert" Text="Insert"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEditScenario" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
<HeaderStyle HorizontalAlign="Left" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
No Applications available!
</EmptyDataTemplate>
</asp:GridView>
--------------------------------CS--------------------------------------------------------
protected void grdScenario_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
DataSet ds = GetData("qtp_getservice");
if (e.Row.RowType == DataControlRowType.DataRow)
{
//services,environment,platform
// bool flag = false;
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
{
Label lbl = (Label)e.Row.FindControl("LblScenarioStatus");
if (null != lbl && lbl.Text != "")
{
lbl.Text = (string.Compare(lbl.Text, "True", true) == 0) ? "Enabled" : "Disabled";
}
}
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
// {
// for (int i = 0; i < grdScenario.Rows.Count ; i++)
// {
// foreach (GridViewRow gvRow in grdScenario.Rows)
{
DropDownList ddlApp = (DropDownList)e.Row.FindControl("ddlAppDataRow");
DropDownList ddlTT = (DropDownList)e.Row.FindControl("ddlTestTypeDataRow");
DropDownList ddlST = (DropDownList)e.Row.FindControl("ddlScenarioTypeDataRow");
//Label lblapp = ((Label)e.Row.Cells[2].FindControl("lblapp"));
//Label lblTestType = ((Label)e.Row.Cells[3].FindControl("lblTestType"));
//Label lblScenarioType = ((Label)e.Row.Cells[4].FindControl("lblScenarioType"));
// Label lblapp = ((Label)grdScenario.Rows[i].Cells[2].FindControl("lblapp"));
//Label lblTestType = ((Label)grdScenario.Rows[i].Cells[3].FindControl("lblTestType"));
// Label lblScenarioType = ((Label)grdScenario.Rows[i].Cells[4].FindControl("lblScenarioType"));
if (ddlApp != null)
{
HiddenField hdnAppid = (HiddenField)e.Row.FindControl("hdnAppid");
ddlApp.DataSource = ds.Tables[4].DefaultView;
ddlApp.DataTextField = "AppName";
ddlApp.DataValueField = "SNO";
ddlApp.DataBind();
//ddlApp.SelectedIndex = ddlApp.Items.IndexOf(ddlApp.Items.FindByText(grdScenario.DataKeys[e.Row.RowIndex].Values[1].ToString()));
//ddlApp.SelectedItem.Text = lblapp.Text.ToString();
ddlApp.SelectedValue = hdnAppid.Value;
}
if (ddlTT != null)
{
HiddenField hdnTesttype = (HiddenField)e.Row.FindControl("hdnTesttype");
ddlTT.DataSource = ds.Tables[5].DefaultView;
ddlTT.DataTextField = "Testingtypedescription";
ddlTT.DataValueField = "TestingTypeID";
ddlTT.DataBind();
//ddlTT.SelectedItem.Text = lblTestType.Text.ToString();
ddlTT.SelectedValue = hdnTesttype.Value;
//ddlTT.SelectedIndex = ddlTT.Items.IndexOf(ddlTT.Items.FindByText(grdservices.DataKeys[e.Row.RowIndex].Values[2].ToString()));
}
if (ddlST != null)
{
HiddenField hdnScenariotype = (HiddenField)e.Row.FindControl("hdnScenariotype");
ddlST.DataSource = ds.Tables[6].DefaultView;
ddlST.DataTextField = "ScenarioTypeDescription";
ddlST.DataValueField = "ScenarioTypeID";
ddlST.DataBind();
ddlST.SelectedValue = hdnScenariotype.Value;
//ddlST.SelectedItem.Text = lblScenarioType.Text.ToString();
//ddlST.SelectedIndex = ddlST.Items.IndexOf(ddlST.Items.FindByText(grdScenario.DataKeys[e.Row.RowIndex].Values[3].ToString()));
}
}
// }
//// }
}
if (e.Row.RowType == DataControlRowType.Footer)
{
//Bind environment,platform at footer.
DropDownList cmbNewType = (DropDownList)e.Row.FindControl("ddlAppFooter");
cmbNewType.DataSource = ds.Tables[4].DefaultView;
cmbNewType.DataBind();
DropDownList cmbNewType1 = (DropDownList)e.Row.FindControl("ddlTestTypeFooter");
cmbNewType1.DataSource = ds.Tables[5].DefaultView;
cmbNewType1.DataBind();
DropDownList cmbNewType2 = (DropDownList)e.Row.FindControl("ddlScenarioTypeFooter");
cmbNewType2.DataSource = ds.Tables[6].DefaultView;
cmbNewType2.DataBind();
}
}
catch (System.Exception) { }
}
I have label within a DetailsView.
I need to change Text property of this Label inside the DetailsView after event ItemInserted has been trigged.
Script does not throws any error, but the Label does not change its text property.
Any ideas what I am doing wrong?
Thanks for your time!
Markup:
<asp:DetailsView ID="uxInsertAuthorInput" runat="server" AutoGenerateRows="False"
DataKeyNames="AuthorId" DataSourceID="EntityDataSourceCreateAuthor" DefaultMode="Insert"
OnItemInserting="uxInsertAuthor_ItemInserting" OnItemInserted="uxInsertAuthorInput_ItemInserted">
<Fields>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="uxFirstNameDisplayer" runat="server" Text='<%# Bind("FirstName") %>' />
</ItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="uxFirstNameInput" runat="server" Text='<%# Bind("FirstName") %>' />
<asp:RequiredFieldValidator ID="uxRequiredFieldValidatorFirstName" runat="server"
ControlToValidate="uxFirstNameInput" ErrorMessage="First Name Field is required">*</asp:RequiredFieldValidator>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="uxLastNameDisplayer" runat="server" Text='<%# Bind("LastName") %>' />
</ItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="uxLastNameInput" runat="server" Text='<%# Bind("LastName") %>' />
<asp:RequiredFieldValidator ID="uxRequiredFieldValidatorLastName" runat="server"
ControlToValidate="uxLastNameInput" ErrorMessage="Last Name Field is required">*</asp:RequiredFieldValidator>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UserId" SortExpression="UserId">
<InsertItemTemplate>
<asp:DropDownList ID="uxUserListSelector" runat="server" />
<asp:Label ID="uxAllert" runat="server" Text="Label" />
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Profile" SortExpression="ContentAuthor">
<ItemTemplate>
<asp:Label ID="uxContentAuthorDisplayer" runat="server" Text='<%# Bind("ContentAuthor") %>' />
</ItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="uxContentAuthorInput" runat="server" Text='<%# Bind("ContentAuthor") %>' />
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="NoteInternal" SortExpression="NoteInternal">
<ItemTemplate>
<asp:Label ID="uxNoteInternalDisplayer" runat="server" Text='<%# Bind("NoteInternal") %>' />
</ItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="uxNoteInternalInput" runat="server" Text='<%# Bind("NoteInternal") %>' />
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
Code-behind:
protected void uxInsertAuthorInput_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
String FirstName = e.Values["FirstName"].ToString();
String LastName = e.Values["LastName"].ToString();
uxMessageDisplayer.Text = string.Format("Author: {0} {1} has been added.", FirstName, LastName);
// problem is here:
Label myAllert = (Label)uxInsertAuthorInput.FindControl("uxAllert");
myAllert.Text = "TEST";
}
you need to use ItemUpdated event of detailsview rather than ItemInserted event
ItemUpdated: Occurs when an Update button within a DetailsView control is clicked, but after the update operation. from here
I am sure this will resolve issue 100%