I have a Dto for phone numbers:
namespace Dto
{
public class PhoneNumberDto
{
public Int16 PhoneTypeId { get; set; }
public string PhoneNumber { get; set; }
}
}
and a list of this Dto:
List<PhoneNumberDto> phonenum = new List<PhoneNumberDto>();
also I have a gridview:
<table style="width: 100%">
<tr>
<td style="direction: rtl;"align="center" class="auto-style8">
<asp:UpdatePanel ID="UpdatePanelSpect1" runat="server">
<ContentTemplate>
<asp:GridView ID="GridViewCertificateType" runat="server" AutoGenerateColumns="False" CellPadding="3" OnRowCommand="GridViewCertificateType_RowCommand" OnRowDataBound="GridView1_RowDataBound" ShowFooter="True" Width="100%" Font-Names="Tahoma" Font-Size="9pt" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Phone Type
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="DDLSpecialty" runat="server" Height="24px" Width="500px">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="LabelC1" runat="server" Text="Label" Visible="False"></asp:Label>
<asp:TextBox ID="TextBox_Phone" runat="server"></asp:TextBox> </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:ImageButton ID="ImageButtonAddSpec" runat="server" ImageUrl="~/ImageWebForm/add.png" Width="18px" OnClick="ImageButtonAddSpec_Click" />
</FooterTemplate>
<ItemTemplate>
<asp:ImageButton ID="ImageButtonDel0" runat="server" ImageUrl="~/ImageWebForm/Delete.png" Width="18px" CommandName="Del" CommandArgument="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="GridViewCertificateType" EventName="RowCommand" />
</Triggers>
</asp:UpdatePanel>
</td>
</tr>
</table>
Now I want to set data to my dropdown list and text box in Gridview from my list of Dot "phonenum",e.g "PhoneTypeId" set to dropdownlist and phoneNumber set to text box
You can direct bind List to your GridView and then either in the OnRowDataBound event or direct in the template you can do something like this.
First in the template.
<asp:TemplateField>
<HeaderTemplate>
Phone Type
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList ID="DDLSpecialty" Selectedvalue='<%#((PhoneNumberDto)Container.DataItem).PhoneTypeId%>' runat="server" Height="24px" Width="500px">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="LabelC1" runat="server" Text="Label"
Visible="False"></asp:Label>
<asp:TextBox ID="TextBox_Phone"
Text='<%#((PhoneNumberDto)Container.DataItem).phoneNumber %>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
Or in the OnRowDataBound event using FindControl() method you can assign value to your control.
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
PhoneNumberDto dtoPhone = (PhoneNumberDto)e.Row.DataItem;
((TextBox)(e.Row.FindControl("txtPhoneNo"))).Text = dtoPhone.PhoneNumber;
((DropDownList)(e.Row.FindControl("DDLSpecialty"))).SelectedValue = dtoPhone.PhoneTypeId;
}
}
I hope from above 2 option you might get your solution...
Related
I have a page with two gridview; one is inside the modal pop up (grdEmpName) and the other is in the page (gvUser). Now, when the user select search button the modal pop up extender will pop up and then search employees. When the user select someone, the selected data should display to the textbox in the another gridview (gvUser).
Everything is working up to selection in modal pop up extender gridview (grdEmpName)but when I select no data pass to the other gridview. (gvUser). I need to display the selected data in the textbox.
ASPX:
<asp:GridView ID="gvUser" runat="server" Width="1024px"
PageSize="10"
AutoGenerateColumns = "False"
AllowSorting="true"
AllowPaging="true"
ShowFooter="True"
OnSorting="gvUser_Sorting"
OnPageIndexChanging="gvUser_PageIndexChanging"
OnRowCreated="gvUser_RowCreated"
CssClass="Grid"
AlternatingRowStyle-CssClass="alt"
PagerStyle-CssClass="pgr"
FooterStyle-HorizontalAlign="center" >
<PagerSettings Mode="NumericFirstLast" PageButtonCount="5" FirstPageImageUrl="~/images/gridview/first_16x16.png" LastPageImageUrl="~/images/gridview/last_16x16.png" />
<Columns>
<asp:TemplateField>
<ItemTemplate >
<asp:ImageButton ID="ibtnEdit" runat="server" ImageUrl="~/images/gridview/file_edit_24x24.png" title = "Edit" _CommandArgument='<%# Eval("USR_ID") & "|Edit|" & CType(Container,GridViewRow).RowIndex & "|-" %>' />
<asp:ImageButton ID="ibtnUpdate" runat="server" ImageUrl="~/images/gridview/file_save_24x24.png" visible="false" title="Update" _CommandArgument='<%# Eval("USR_ID") & "|Update|" & CType(Container,GridViewRow).RowIndex & "|-" %>' />
<asp:ImageButton ID="ibtnCancel" runat="server" ImageUrl="~/images/gridview/file_cancel_24x24.png" Visible="false" title="Cancel" _CommandArgument='<%# Eval("USR_ID")& "|Cancel|" & CType(Container,GridViewRow).RowIndex & "|-" %>' />
<asp:ImageButton ID="ibtnDelete" runat="server" ImageUrl="~/images/gridview/file_delete_24x24.png" title="Delete" _CommandArgument='<%# Eval("USR_ID")& "|Delete|" & CType(Container,GridViewRow).RowIndex & "|" & Eval("USR_IDNTY") %>' />
<asp:ImageButton ID="btnSearchUser" runat="server" ImageUrl="~/images/gridview/file_search_24x24.png" title="Search" _CommandArgument= "btnSearchUser" OnClick = "btnSearchUser_Click" />
</ItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="ibtnAdd" runat="server" ImageUrl="~/images/gridview/file_add_24x24.png" title="Add" _CommandArgument='<%# Eval("USR_ID") & "|Add|" & CType(Container,GridViewRow).RowIndex & "|-" %>' />
</FooterTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" Width="50px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="User Identity" visible ="false" >
<ItemTemplate>
<asp:Label ID="lblUSR_IDNTY" runat="server" Text='<%# Bind("USR_IDNTY")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="lblUSR_IDNTY" runat="server" Width="95%" Text='<%# Bind("USR_IDNTY")%>' />
</EditItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="User ID" SortExpression="USR_ID">
<ItemTemplate>
<asp:Label ID="lblUSR_ID" runat="server" Text='<%# Bind("USR_ID")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="txtUSR_ID" runat="server" Width="95%" Text='<%# Bind("USR_ID")%>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtUSR_ID" runat="server" Width="95%"/>
</FooterTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="NAME">
<ItemTemplate>
<asp:Label ID="lblNAME" runat="server" Text='<%# Bind("NAME")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNAME" runat="server" Width="95%" Text='<%# Bind("NAME")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNAME" runat="server" Width="95%"/>
</FooterTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Email" SortExpression="EMAIL">
<ItemTemplate>
<asp:Label ID="lblEMAIL" runat="server" Text='<%# Bind("EMAIL")%>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEMAIL" runat="server" Width="95%" Text='<%# Bind("EMAIL")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEMAIL" runat="server" Width="95%"/>
</FooterTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Active" SortExpression="ACTIVE">
<ItemTemplate>
<asp:CheckBox ID="chkACTIVE" runat="server" Checked ='<%# Bind("ACTIVE")%>' Enabled="false" />
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="chkACTIVE" runat="server" Width="95%" Checked ='<%# Bind("ACTIVE")%>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:CheckBox ID="chkACTIVE" runat="server" Width="95%"/>
</FooterTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle HorizontalAlign="Center" Wrap="False"/>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<div style="height:315px;border-style:hidden;" >
<table class="table" >
<tr>
<td style="padding:5px;" >
<img id="Img1" style="height:24px;width:24px; " runat="server" src="images/no_data_64x64.png" />
</td>
<td style="padding:5px;font-size:large;">
<b>No data found.</b>
</td>
</tr>
</table>
</div>
</EmptyDataTemplate>
<PagerStyle CssClass="pgr" />
<AlternatingRowStyle CssClass="alt" />
<FooterStyle HorizontalAlign="Center" />
<EmptyDataRowStyle CssClass="EmptyData" />
</asp:GridView>
</div>
<%-- Picker--%>
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</cc1:ToolkitScriptManager>
<cc1:ModalPopupExtender runat="server" TargetControlID="hdControl" ID="mdlEmpListProcess" PopupControlID="empPanel" CancelControlID="btnClose" OnCancelScript="getFlickerSolved();" >
</cc1:ModalPopupExtender>
<asp:HiddenField runat="server" ID="hdControl" />
<asp:Panel runat="server" ID="empPanel" Width="909px" Height="300" BorderStyle="Ridge" BorderColor="Gray" Style="display:none;" BackColor="Control">
<table>
<tr>
<td>
<asp:TextBox runat="server" id="txtEmpName" CssClass="textbox" Width="240"></asp:TextBox>
<asp:Button runat="server" ID="btnSearchEmp" CssClass="button" Text="SEARCH" OnClientClick="getFlickerSolved();"/>
<asp:Button runat="server" ID="btnClose" CssClass="button" Text="CLOSE" />
</td>
</tr>
</table> <br />
<div style="overflow:scroll; width:882px; height:233px;">
<asp:GridView runat="server" ID="grdEmpName" Width="863px" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" Height="35px" GridLines="Vertical">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton runat="server" ID="optEmp" AutoPostBack="true" _CommandArgument= "Select" OnCheckedChanged="selectedEmployee" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="USR_ID" HeaderText="USER ID"/>
<asp:BoundField DataField="NAME" HeaderText="NAME"/>
<asp:BoundField DataField="EMAIL" HeaderText="EMAIL ADDRESS">
<HeaderStyle Width="300px" />
</asp:BoundField>
</Columns>
<RowStyle CssClass="rowStyle" />
<HeaderStyle CssClass="headerStyle" BackColor="Black" Font-Bold="True" ForeColor="White" />
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
</div>
</asp:Panel>
<%-- End Picker--%>
Now, here's may code for selectedEmployee (Radio Button)
Protected Sub selectedEmployee(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lnBTNDone As RadioButton = DirectCast(sender, RadioButton)
Dim row As GridViewRow = DirectCast(lnBTNDone.NamingContainer, GridViewRow)
Dim RowIndex As Integer = gvUser.EditIndex
Dim dt As New DataTable
dt = TryCast(Session("dbCache_User"), DataView).Table.Copy
Dim dv As New DataView(dt, "", "USR_IDNTY", DataViewRowState.OriginalRows)
Dim s As String = TryCast(gvUser.Rows(RowIndex).FindControl("lblUSR_IDNTY"), Label).Text
Dim idx As Integer = dv.Find(s)
dv(idx)("USR_ID") = row.Cells(1).Text
dv(idx)("NAME") = row.Cells(2).Text
dv(idx)("EMAIL") = row.Cells(3).Text
gvUser.DataSource = dv
gvUser.DataBind()
Me.mdlEmpListProcess.Hide()
End Sub
But I've encounter this error when debugging system.data.dataexception was unhandled by user code. Cannot set Name when passing the value.
dv(idx)("NAME") = row.Cells(2).Text
as per you comment NAME is readonly field error given at runtime than you make is readonly false as below , add same line in your code after reading value
dt.Columns["NAME"].ReadOnly = false
Working example
DataTable dataTable = new DataTable("dataTable");
DataColumn dataColumn = new DataColumn("dataColumn");
dataTable.Columns.Add(dataColumn);
// Add ten rows.
DataRow dataRow;
for (int i = 0; i < 10; i++)
{
dataRow = dataTable.NewRow();
dataRow["dataColumn"] = "item " + i;
dataTable.Rows.Add(dataRow);
}
dataTable.AcceptChanges();///add this line in your code
var dv = new DataView(dataTable, "", "dataColumn", DataViewRowState.OriginalRows);
int index = dv.Find("item 1");
if (index == -1)
Console.WriteLine("Product not found");
else
dv[index]["dataColumn"] = "item 1000";
I suggest first check you are able to find row or not
int idx = dv.Find(s);
if (index == -1)
{
Console.WriteLine ("Product not found");
}
else
{
dv[index]["columnname"] = "value";
}
I have an ADD button in my FormView in ASP.NET. And I have txtName on it that should first check if the textbox is empty. If it was empty, the ADD button should message Please fill up the form.
I tried the traditional error handling in vb.net like
If txtbox.text = "" then
error msg
end if
but it is not applicable when making a web page using ASP.NET backend: VBNET.
<asp:FormView ID="FormView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource1" Height="92px" Width="835px" AllowPaging="True" CellPadding="3" DefaultMode="Insert" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" GridLines="Horizontal" BackImageUrl= "howto.jpg" style="margin-bottom: 12px">
<EditRowStyle BackColor="#738A9C" BorderStyle="Solid" Font-Bold="True" ForeColor="#F7F7F7" />
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#F7F7F7" />
<InsertItemTemplate>
<asp:Label ID="lblName" runat="server" Text="Name of Applicant" Font-Size ="10pt" Font-Names="Arial" ></asp:Label>
<asp:Label ID="lblBday" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Birthday"></asp:Label>
<asp:Label ID="Gender" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Gender"></asp:Label>
<br />
<asp:TextBox ID="txtName" runat="server" Height="19px" Text='<%# Bind("Name") %>' Width="216px" Font-Size ="10" Font-Names="Arial"></asp:TextBox>
<asp:TextBox ID="txtBday" runat="server" Font-Names="Arial" Font-Size="10pt" Height="19px" Text='<%# Bind("Birthday") %>' TextMode="Date" Width="216px"></asp:TextBox>
<asp:DropDownList ID="txtGender" runat="server" Height="19px" Width="222px" SelectedValue='<%# Bind("Gender") %>'>
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:Label ID="lblAddress" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Address"></asp:Label>
<asp:Label ID="lblContact" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Contact"></asp:Label>
<br />
<asp:TextBox ID="txtAddress" runat="server" Font-Names="Arial" Font-Size="10pt" Height="19px" Text='<%# Bind("Address") %>' Width="488px"></asp:TextBox>
<asp:TextBox ID="txtContact" runat="server" Font-Names="Arial" Font-Size="10pt" Height="19px" Text='<%# Bind("Contact") %>' Width="216px"></asp:TextBox>
<br />
<br />
<br />
<asp:Label ID="lblEduc" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Educational Attainment"></asp:Label>
<asp:Label ID="lblCourse" runat="server" Font-Names="Arial" Font-Size="10pt" Text="Course"></asp:Label>
<br />
<asp:DropDownList ID="txtEduc" runat="server" Height="19px" SelectedValue='<%# Bind("Education") %>' Width="495px">
<asp:ListItem>High School</asp:ListItem>
<asp:ListItem>College Diploma (2-3 years)</asp:ListItem>
<asp:ListItem>College Undergrad (4-5 years)</asp:ListItem>
<asp:ListItem>Bachelor's Degree</asp:ListItem>
<asp:ListItem Value="Master">Masteral</asp:ListItem>
<asp:ListItem>Doctor</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtCourse" runat="server" Font-Names="Arial" Font-Size="10pt" Height="19px" Text='<%# Bind("Course") %>' Width="216px"></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="btnAdd" runat="server" Text="ADD" Font-Bold="True" Font-Size="Medium" ForeColor="Black" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" CausesValidation="True" ValidationGroup="Insert" Font-Names="Berlin Sans FB Demi" CommandName="Insert" />
<asp:Button ID="btnCancel" runat="server" Text="CANCEL" Font-Bold="True" Font-Names="Berlin Sans FB Demi" Font-Size="Medium" ForeColor="Black" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" CommandName="Cancel" />
</InsertItemTemplate>
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right" />
<RowStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" />
</asp:FormView>
You can't show that alert on a webapp, but you can use javascript in order to do it.
In code behind declare this function:
Public Sub AlertMessage(ByVal mensaje As String, ByVal pag As Page)
pag.ClientScript.RegisterStartupScript(Me.GetType(), "showConfirm", "Alerta('" & mensaje & "');", True)
End Sub
Then call it from your ADD button like this:
If txtbox.text = "" then
AlertMessage(msg, Me.Page)
end if
Hope this helps
I've learn from some research that there is other way for error handling using ASPNET. I used the validation control and validation group.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" ValidationGroup ="FillUp" runat="server" ControlToValidate ="txtName" ErrorMessage="Please Enter Your Name"></asp:RequiredFieldValidator><br />
<asp:Button ID="btnAdd" runat="server" Text="ADD" Font-Bold="True" Font-Size="Medium" ForeColor="Black" BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" CausesValidation="True" ValidationGroup="FillUp" Font-Names="Berlin Sans FB Demi" CommandName="Insert" />
enter image description here
I also add this code in my Web.config
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
I know, what you are thinking,"This guy has a poor desig", lol, well the important is you know the code xD
I'm working with my GridView table in ASP.Net.
First when I click the Edit link button in my GridView I will be able to edit it with Textbox and dropdownlist.
Here's the image of my GridView.
So the Business Unit was enabled but the Division, Sub-Division, Classification and Sub-Classification are not.
My problem is that when I select the Business Unit, the dropdownlist Division should be enabled and be able to select the value according to the Business Unit that I selected and so on. So the next dropdownlist will be dependent on the first dropdownlist that you selected. I searched about this on the internet already and tried different solutions but I'm still confused and don't know how it will work.
Here's my code in ASP.Net
<asp:GridView ID="gvGroup" runat="server" AllowPaging="false"
AllowSorting="true" AutoGenerateColumns="False" BorderColor="Silver"
BorderWidth="1px" Height="104px" ShowFooter="true" ShowHeader="true"
style="margin-right: 0px" Width="77%">
<RowStyle Font-Names="Arial" Font-Size="9pt" HorizontalAlign="Center" />
<Columns>
<asp:TemplateField HeaderStyle-BackColor="#666666"
HeaderStyle-ForeColor="White" HeaderStyle-Width="10px" HeaderText=""
ItemStyle-Width="10px">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
<HeaderStyle BackColor="#666666" ForeColor="White" />
<ItemStyle Width="10px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="140px" Visible="false">
<ItemTemplate>
<asp:Label ID="lblMasterID" runat="server" Text='<%#Bind("MASTERID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="" ItemStyle-Width="140px" Visible="false">
<ItemTemplate>
<asp:Label ID="lblSEQID" runat="server" Text='<%#Bind("SEQID") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Job Title" ItemStyle-Font-Size="9pt"
ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblJobTitle" runat="server" Text='<%#Bind("JOBTITLE") %>'
Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtJT" runat="server" CssClass="ehr_textbox"
Text='<%#BIND("JOBTITLE") %>' Width="140px"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtJobTitleAdd" runat="server" Width="110px">
</asp:TextBox>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Business Unit"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblBusinessUnit" runat="server"
Text='<%#Bind("BUSINESS_UNIT") %>' Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvBusinessUnit" runat="server" CssClass="ehr_dropdown"
Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlBusinessUnitAdd" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Division" ItemStyle-Font-Size="9pt"
ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblDivision" runat="server" Text='<%#Bind("DIVISION") %>'
Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvDivision" Enabled="false" runat="server" CssClass="ehr_dropdown"
Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlDivisionAdd" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Sub-Division"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblSubDivision" runat="server" Text='<%#Bind("SUB_DIVISION") %>'
Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvSubDivision" Enabled="false" runat="server" CssClass="ehr_dropdown"
Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSubDivisionAdd" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Classification"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblClassification" runat="server"
Text='<%#Bind("CLASSIFICATION") %>' Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvClassification" Enabled="false" runat="server"
CssClass="ehr_dropdown" Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlClassification" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" HeaderStyle-Font-Size="9pt"
HeaderStyle-ForeColor="White" HeaderText="Sub-Classification"
ItemStyle-Font-Size="9pt" ItemStyle-Width="140px">
<ItemTemplate>
<asp:Label ID="lblSubClassification" runat="server"
Text='<%#Bind("SUB_CLASSIFICATION") %>' Width="140px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_gvSubClassification" Enabled="false" runat="server"
CssClass="ehr_dropdown" Width="140px">
</asp:DropDownList>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlSubClassification" runat="server" Width="110px">
</asp:DropDownList>
</FooterTemplate>
<HeaderStyle BackColor="#666666" Font-Size="9pt" ForeColor="White" />
<ItemStyle Font-Size="9pt" Width="140px" />
</asp:TemplateField>
<asp:TemplateField HeaderStyle-BackColor="#666666" ItemStyle-Font-Size="9pt">
<ItemTemplate>
<asp:LinkButton ID="lnkRemove" runat="server" CommandName="Delete"
OnClientClick="return confirm('Do you want to delete?')" Text="Delete"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="btnAdd" runat="server" CommandName="Add"
OnClick="AddgvGroup" Text="Add"></asp:LinkButton>
</FooterTemplate>
<HeaderStyle BackColor="#666666" />
<ItemStyle Font-Size="9pt" />
</asp:TemplateField>
<asp:CommandField HeaderStyle-BackColor="#666666" HeaderStyle-Width="10px"
ItemStyle-Font-Size="9pt" ItemStyle-Width="10px" ShowEditButton="True">
<HeaderStyle BackColor="#666666" />
<ItemStyle Font-Size="9pt" />
</asp:CommandField>
</Columns>
<PagerStyle Font-Size="9pt" HorizontalAlign="Right" />
<EmptyDataTemplate>
<div style="width: 100%; font-size: 10pt; text-align: center; color: Red;">
No record found.
</div>
</EmptyDataTemplate>
<HeaderStyle BackColor="DarkGray" Font-Bold="True" Font-Names="Arial"
Font-Size="9pt" ForeColor="White" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="Gainsboro" />
</asp:GridView>
And here's my code in VB.Net
Protected Sub gvGroup_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvGroup.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
If (e.Row.RowState And DataControlRowState.Edit) > 0 Then
Dim ddlBusinessUnit As DropDownList = DirectCast(e.Row.FindControl("ddl_gvBusinessUnit"), DropDownList)
'bind dropdown-list
Dim sqlstr As String
Dim dt As DataTable = New DataTable()
sqlstr = "Select BUSINESS_UNIT from BUSINESS_UNIT_TBL"
dt = ehr_utils.DataTable(sqlstr)
ddlBusinessUnit.DataSource = dt
ddlBusinessUnit.DataTextField = "BUSINESS_UNIT"
ddlBusinessUnit.DataValueField = "BUSINESS_UNIT"
ddlBusinessUnit.DataBind()
Dim dr As DataRowView = TryCast(e.Row.DataItem, DataRowView)
ddlBusinessUnit.SelectedValue = dr("BUSINESS_UNIT").ToString()
End If
End If
End Sub
Any idea of how can I possibly do that? And I tried also the OnSelectedIndexChanged
First you need to add an OnSelectedIndexChanged to ddl_gvBusinessUnit and set AutoPostBack to true
<asp:DropDownList ID="ddl_gvBusinessUnit" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_gvBusinessUnit_SelectedIndexChanged">
And then in code behind
protected void ddl_gvBusinessUnit_SelectedIndexChanged(object sender, EventArgs e)
{
//cast the sender back to a dropdownlist
DropDownList dropDownList1 = sender as DropDownList;
//get the selectedvalue
string value = dropDownList1.SelectedValue;
//find the other dropdownlist in the correct row by using the editindex
DropDownList dropDownList2 = gvBusinessUnit.Rows[gvBusinessUnit.EditIndex].FindControl("DropDownList2") as DropDownList;
//do stuff with the other dropdownlist, like give it a color or add database values based on the first dropdownlist
dropDownList2.BackColor = Color.Red;
}
VB
Protected Sub ddl_gvBusinessUnit_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
'cast the sender back to a dropdownlist
Dim dropDownList1 As DropDownList = CType(sender,DropDownList)
'get the selectedvalue
Dim value As String = dropDownList1.SelectedValue
'find the other dropdownlist in the correct row by using the editindex
Dim dropDownList2 As DropDownList = CType(gvBusinessUnit.Rows(gvBusinessUnit.EditIndex).FindControl("DropDownList2"),DropDownList)
'do stuff with the other dropdownlist, like give it a color or add database values based on the first dropdownlist
dropDownList2.BackColor = Color.Red
End Sub
I can't get the value. What I do get is a blank value or videoName = " ". How can I get the value of videoName? In the rowdeleting event, I am using Rows(e.Index).Cells(2).Text to get the value but it is blank. Is there another way to get the field, "videoname"?
Protected Sub GridView1_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
Dim videoName As String = gridview1.Rows(e.RowIndex).Cells(2).Text
Dim val As String = videoName
If File.Exists(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) + "\contents\published" + videoName) Then
File.Delete(HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath) + "\contents\published" + videoName)
End If
End Sub
<asp:GridView id="GridView1" runat="server" Width="680px" GridLines="None" DataSourceID="SqlDataSource2" DataKeyNames="id" CellSpacing="1" CellPadding="3" BorderWidth="2px" BorderStyle="Ridge" BorderColor="White" AutoGenerateColumns="False"
AllowPaging="True" AllowSorting="True" EmptyDataText="No Record Found" OnRowDeleting="GridView1_RowDeleting" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="9" >
<Columns>
<asp:BoundField HeaderText="Id" DataField="Id" ReadOnly="true" visible="false" />
<asp:BoundField HeaderText="CustomerID" DataField="CustomerID" ReadOnly="true" visible="false"/>
<asp:BoundField HeaderText="VideoName" DataField="VideoName" ReadOnly="true" visible="false"/>
<asp:TemplateField>
<HeaderStyle Width="5%" />
<ItemStyle Width="5%" />
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Runat="server" OnClientClick="return confirm('Are you sure you want to delete this video?');"
CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Clip" SortExpression="ThumbName">
<HeaderStyle Width="5%" />
<ItemStyle Width="5%" />
<ItemTemplate>
<div style="margin:2px; width:133px; background-color:rgb(68,68,68); -moz-box-shadow: 5px 5px 5px rgba(68,68,68,0.6); -webkit-box-shadow:5px 5px 5px rgba(68,68,68,0.6);box-shadow:5px 5px 5px rgba(68,68,68,0.6); zoom: 1;">
<asp:hyperlink id="link" NavigateUrl='<%# Eval("VideoName", "~/Test/playVideos2.aspx?FileName={0}&Thumb=" + Eval("ThumbName") + "&Duration=" + Eval("Duration"))%>' runat="server">
<asp:image id="img" ImageUrl='<%# String.Format("~/contents/thumbs/{0}",Eval("ThumbName"))%>' width="130" height="80" runat="server" />
</asp:hyperlink>
<asp:Label ID="lblMovieName" Text='<%#Bind("VideoName") %>' runat="server"></asp:Label>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date">
<HeaderStyle Width="15%" />
<ItemStyle Width="15%" />
<ItemTemplate>
<asp:Label ID="date" runat="server" Text='<%# Bind("DateCreated") %>'></asp:Label><br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderStyle Width="75%" />
<ItemStyle Width="75%" />
<ItemTemplate>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
</asp:GridView>
This is prone to erros when you add or delete a column from gridview in future your code would fail again. I suggest you use a label inside template field for your MovieName field & use FindControl to find your label.
Aspx
<asp:TemplateField HeaderText="Movie Name" >
<ItemTemplate>
<asp:Label ID="lblMovieName" Text='<%#Bind("MovieName") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Code behind
protected void gvCustomer_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label lblMovieName = gvCustomer.Rows[e.RowIndex].FindControl("yourLableControlName") as Label;
// Perform your delete
}
Updated
I found that in your code you have the movieName Visibleproperty set to false. This will prevent it from rendering on page. You can either store values in DataKeys or remove visible=false to fetch desired value.
I have a Grid view in which a text box is present in footer and edit template from where user can update data. i made a table inside a empty template of a grid and add a server side text box on it so that user can insert data in a grid if there is no record present inside a grid.
There is a server side button present out side the grid, on clicking of that button i m saving the data that user insert or update via grid. Now i am facing the problem to find out the control present inside the empty template of the grid while i m saving the data.
My code looks like as :
<asp:GridView ID="grwBrandInfo" runat="server" Width="100%" HeaderStyle-CssClass="gridheaderstyle"
GridLines="Vertical" AutoGenerateColumns="false" ShowFooter="True" BackColor="White"
OnRowCommand="grwBrandInfo_RowCommand" OnRowEditing="grwBrandInfo_RowEditing"
OnRowDeleting="grwBrandInfo_RowDeleting" OnRowUpdating="grwBrandInfo_RowUpdating"
OnRowDataBound="grwBrandInfo_RowDataBound" OnRowCancelingEdit="grwBrandInfo_RowCancelingEdit"
BorderStyle="None">
<Columns>
<asp:TemplateField HeaderText="Quarts">
<ItemTemplate>
<%#Eval("QUARTS")%>
<asp:HiddenField ID="hdnPERMIT_QUANTITY_ID" runat="server" Value='<%#Eval("PERMIT_QUANTITY_ID") %>' />
<asp:HiddenField ID="hdnQuarts" runat="server" />
</ItemTemplate>
<FooterTemplate>
<asp:HiddenField ID="hdnPERMIT_QUANTITY_ID" runat="server" Value='<%#Eval("PERMIT_QUANTITY_ID") %>' />
<asp:TextBox ID="txtFootQuarts" runat="server" CssClass="txtfields" MaxLength="4"></asp:TextBox>
</FooterTemplate>
<EditItemTemplate>
<asp:HiddenField ID="hdnPERMIT_QUANTITY_ID" runat="server" Value='<%#Eval("PERMIT_QUANTITY_ID") %>' />
<asp:TextBox ID="txtEditQuarts" runat="server" EnableViewState="false" Text='<%#Eval("QUARTS")%>'
CssClass="txtfields" MaxLength="4"></asp:TextBox>
</EditItemTemplate>
<HeaderStyle HorizontalAlign="Center" />
<ItemStyle HorizontalAlign="Center" />
<FooterStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:LinkButton ID="lbtnInsert" runat="server" Text="Insert" CssClass="hypfield"
CommandName="Insert" ValidationGroup="save"></asp:LinkButton>
</FooterTemplate>
<FooterStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" HeaderText="Action" ShowDeleteButton="true"
ValidationGroup="save">
<ItemStyle CssClass="hypfield" HorizontalAlign="Center" VerticalAlign="Top" />
<ControlStyle CssClass="hypfield" />
<HeaderStyle HorizontalAlign="Center" />
</asp:CommandField>
</Columns>
<EmptyDataTemplate>
<table border="1" cellpadding="5" cellspacing="0" width="100%">
<tr align="Left" bgcolor="#726E6D">
<td>
<b>
<asp:HiddenField ID="hdnPERMIT_QUANTITY_ID" runat="server" Value='0' />
<asp:Label ID="lblQuarts" runat="server" ForeColor="white" Text="Quarts" CssClass="lblfields"></asp:Label></b>
</td>
</tr>
<tr align="left" id="trEntryNotCompleted" runat="server" bgcolor="#FFFFEE">
<td>
<asp:TextBox ID="txtQuarts" runat="server" CssClass="txtfields5char" MaxLength="4"></asp:TextBox>
</td>
</tr>
</table>
</EmptyDataTemplate>
<FooterStyle BackColor="#CCCC99" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#6B696B" CssClass="gridheaderstyle" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:Button ID="btnSave" runat="server" Text="Save" ValidationGroup="save" OnClick="btnSave_Click" />
So the problem is that how did i find the value of txtQuarts present in the empty template when i am saving data
Dim txtId As String = CType(gridHelpDesk.Controls(0).Controls(0).FindControl("txtId"), TextBox).Text