How to fill DetailsViewUpdateEventArgs.OldValues when using TemplateField - c#

I have a DetailsView control that's filled with a LinqDataSource without problems.
The user needs to upload a file when updating the information, so, I created a TemplateField with an asp:FileUpload control inside the EditItemTemplate (the itemTemplate is just an asp label).
The problem is that when I try to update the data, in the DetailsView.OnItemUpdating, the DetailsViewUpdateEventArgs.OldValues does not contain all the old values (nor the keys). Actually, it just has the ArticleTitle and ArticleNumber.
I thougth it was because the otheres where TemplateFields, so I changed the ArticleDesc from BoundField to TemplateField, added a Multiline Textbox to the EditItemTemplate and an asp Label to the ItemTemplate, and it worked fine (the original value of ArticleDesc was there).
This problem can be solved by other ways, like querying again for the data, but I don't like that solution. Besides, I want to understand why it happens.
ASPX Page
<asp:DetailsView ID="ModArticleDV" runat="server" AllowPaging="True" OnItemUpdated="ModArticleDV_ItemUpdated" OnItemUpdating="ModArticleDV_ItemUpdating" OnModeChanged="ModArticleDV_ModeChanged" OnDataBound="ModArticleDV_DataBound" OnDataBinding="ModArticleDV_DataBinding" AutoGenerateRows="False" DataSourceID="ArticleDS" DataKeyNames="ArticleID">
<Fields>
<asp:BoundField DataField="ArticleID" HeaderText="ID" SortExpression="ArticleID" ReadOnly="true" />
<asp:BoundField DataField="ArticleNumber" HeaderText="Número" SortExpression="ArticleNumber" />
<asp:BoundField DataField="ArticleTitle" HeaderText="Título" SortExpression="ArticleTitle" ConvertEmptyStringToNull="false" />
<asp:TemplateField HeaderText="Archivo PDF" SortExpression="ArticleFile">
<EditItemTemplate>
Archivo Actual: <asp:Label ID="EditCurrentFileLbl" Text='<%# Eval("ArticleFile") %>' runat="server" /><br />
<label class="upload-file">Nuevo Archivo</label>: <asp:FileUpload ID="NewFileUpload" runat="server" CssClass="file-upload" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="CurrentFileLbl" runat="server" Text='<%# Eval("ArticleFile") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Descripción" ConvertEmptyStringToNull="false">
<EditItemTemplate>
<asp:TextBox ID="NewDescription" TextMode="MultiLine" Text='<%# Eval("ArticleDesc") %>' runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="CurrentDescription" Text='<%# Eval("ArticleDesc") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cantidad de Páginas" SortExpression="ArticlePageCount" ConvertEmptyStringToNull="false">
<EditItemTemplate>
<asp:UpdatePanel ID="CountPagesUP" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="CountPagesLink" EventName="Click" />
</Triggers>
<ContentTemplate>
Páginas: <asp:Label runat="server" ID="PageCountLabel" Text='<%# Eval("ArticlePageCount") %>' />
</ContentTemplate>
</asp:UpdatePanel>
<asp:LinkButton ID="CountPagesLink" runat="server" OnClick="CountPagesLink_Click">Contar Páginas</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="CurrentPageCountLbl" Text='<%# Eval("ArticlePageCount") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pertenece al" SortExpression="MagazineID">
<EditItemTemplate>
<asp:DropDownList ID="NewMagazine" runat="server" DataSourceID="MagazineDS" DataTextField="MagazineTitle" DataValueField="MagazineID" OnPreRender="NewMagazine_PreRender"></asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="CurrentMagazineLbl" runat="server" OnPreRender="CurrentMagazineLbl_PreRender" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Autores" SortExpression="ArticleID">
<EditItemTemplate>
<label>Filtrar Autor</label>: <input type="text" class="search-in-list" title="Reduce la lista de autores. Ingrese el appellido paterno, materno y/o nombre. Debe incluir tildes de ser necesario." /><br />
<asp:ListBox ID="NewAuthors" runat="server" CssClass="list-to-search" SelectionMode="Multiple" DataSourceID="AuthorDS" DataTextField="AuthorFullName" DataValueField="AuthorID" Height="206px" Width="292px" OnDataBound="NewAuthors_DataBound"></asp:ListBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="CurrentAuthorsLbl" runat="server" OnPreRender="CurrentAuthorsLbl_PreRender" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="MagazineID" Text='<%# Eval("MagazineID") %>' runat="server" />
<asp:Label ID="CurrentAuthorID" runat="server" OnPreRender="CurrentAuthorID_PreRender" />
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" UpdateText="Guardar" CancelText="Cancelar" SelectText="Seleccionar" NewText="Nuevo" InsertText="Agregar" EditText="Editar" DeleteText="Borrar" ControlStyle-CssClass="command-field-control" />
</Fields>
</asp:DetailsView>
<asp:LinqDataSource ID="ArticleDS" runat="server" ContextTypeName="LinqToSql.FinderSchemaDataContext" EnableUpdate="True" OrderBy="ArticleID" TableName="Articles" EnableDelete="True" EntityTypeName=""></asp:LinqDataSource>
<asp:LinqDataSource ID="MagazineDS" runat="server" ContextTypeName="LinqToSql.FinderSchemaDataContext" EntityTypeName="" OrderBy="PublishYear, MagazineNumber" TableName="Magazines" Select="new (MagazineID, MagazineNumber, MagazineName, PublishYear, String.Format("Volumen {0} del año {1}", MagazineNumber.ToString(), PublishYear.ToString()) AS MagazineTitle)"></asp:LinqDataSource>
<asp:LinqDataSource ID="AuthorDS" runat="server" ContextTypeName="LinqToSql.FinderSchemaDataContext" EntityTypeName="" OrderBy="AuthorFName, AuthorMName, AuthorName" TableName="Authors" Select="new (AuthorID, AuthorFName, AuthorMName, AuthorName, String.Format("{0} {1} {2}", AuthorFName.ToString(), AuthorMName.ToString(), AuthorName.ToString()) AS AuthorFullName)" />
Code Behind
protected void ModArticleDV_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
{
Messages.InnerHtml = "";
try
{
Messages.InnerHtml += "<h3>Old Values</h3><br />";
foreach (DictionaryEntry de in e.OldValues)
{
Messages.InnerHtml += String.Format("Key: {1} - Value: {0}<br />", de.Value, de.Key);
}
Messages.InnerHtml += "<h3>New Values</h3><br />";
foreach (DictionaryEntry de in e.NewValues)
{
Messages.InnerHtml += String.Format("Key: {1} - Value: {0}<br />", de.Value, de.Key);
}
e.Cancel = false;
}
catch (Exception ex)
{
Messages.InnerHtml += ex.Message;
}
}
This shows me only the ArticleNumber and ArticleTitle
What Have I tried?
Using Bind instead of Eval
Using a default DetailsView and LinqDataSource fills the e.OldValues with the original values, but I didn't notice the difference :(.
I have read many times the MSDN documentation on DetailsView, DetailsViewUpdateEventArgs and many other resources, but none gave me a hint (I read somewhere that it may be because of the TemplateField(s), but in the tests I run, they work ok).
This question is related, but that's the way I've always added data to TemplateField.
If someone could give me something to read to understand why it happens or if someone could tell me where to fill the OldValues (other than the ItemUpdating event), it would be great :).
Thanks in advance.

I confirm that the dictionaries e.(New|Old)Values are not filled with the data in TemplateField(s).
My solution (I don't know of any better) was to use a hidden TemplateField (Visible="false") and, inside the ItemTemplate, add the original values with labels (asp:Label) then access them using DetailsView.FindControl("control_id"):
<TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="OriginalValue1" runat="server" Text='<%# Eval("Column1") %>' />
<asp:Label ID="OriginalValueN" runat="server" Text='<%# Eval("ColumnN") %>' />
</ItemTemplate>
</TemplateField>
And in the Code Behind:
// DetailsView.ID = "MyDetailsView";
Label original1 = (Label)MyDetailsView.FindControl("OriginalValue1");
Label originalN = (Label)MyDetailsView.FindControl("OriginalValueN");
For some reason I can't explain, some test I've run with TemplateFields worked (the data was added to the dictionary without my intervention), that's why I was confused and thought it was an error.
Anyway, the problem is now solved and I hope this helps someone :).

Related

How to fill data in some of GridViewRow and leave some blank

ASP.net C#
I am creating a gridview having 5 rows containing detail of operations users have to perform. Detail of operations should be filled when it is completed. At a time any number of operation details can be filled.
In gridview, first Item-template contains Label (for operation name) and others are textbox (for other details).
If any user has filled 3 rows then rest of two rows should be blank.
My problem is how to bind those 3 rows filled previously leaving two bottom rows available for entry.
My Gridview design is :
<asp:GridView CssClass="table-bordered gridStyle" runat="server" ShowFooter="True"
ID="grdOperationEntry" GridLines="None" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Operation">
<ItemTemplate>
<asp:Label Text='<%# Eval("operation_title") %>' ID="lblOperationName" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date of Completion">
<ItemTemplate>
<asp:TextBox runat="server" Text='<%# Eval("date_completed") %>' CssClass="form-control"
ID="txtDateCompletion" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Time Taken">
<ItemTemplate>
<asp:TextBox runat="server" Text='<%# Eval("time_taken") %>' ID="txtTimeTaken" CssClass="form-control" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Score">
<ItemTemplate>
<asp:TextBox runat="server" Text='<%# Eval("score_gain") %>' ID="txtScore" CssClass="form-control" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Reported To">
<ItemTemplate>
<asp:TextBox runat="server" Text='<%# Eval("reported_to") %>' ID="txtReportedTo"
CssClass="form-control" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#7C6F57" />
</asp:GridView>
Add a Label with all TextBoxes in GridView and set them visible false and bind also Label from database. I have added an example TemplateField to below, you can do it for all other:
.....
<asp:TemplateField HeaderText="Date of Completion">
<ItemTemplate>
<asp:Label runat="server" Visible="False" Text='<%# Eval("date_completed")
ID="lblDateCompletion" %>'></asp:Label>
<asp:TextBox runat="server" Visible="False" Text='<%# Eval("date_completed") %>'
ID="txtDateCompletion" />
</ItemTemplate>
</asp:TemplateField>
.....
In RowDataBound event set them visible true:
protected void grdOperationEntry_RowDataBound(object sender, GridViewRowEventArgs e)
{
// check if gridview row not a header or footer
if (e.Row.RowType == DataControlRowType.DataRow)
{
// get controls by id from gridview and cast them
Label lblDateCompletion = e.Row.FindControl("lblDateCompletion") as Label;
TextBox txtDateCompletion = e.Row.FindControl("txtDateCompletion") as TextBox;
if (lblDateCompletion.Text == null)
txtDateCompletion.Visible = true;
else
lblDateCompletion.Visible = true;
// perform same for other controls
}
}
Note: Don't forget to add OnRowDataBound property to your GridView <asp:GridView ID="grdOperationEntry" runat="server" OnRowDataBound="grdOperationEntry_RowDataBound" >

Databinding issue with Eval/Bind - C# Webforms asp.net

I'm working on an existing project, doing some updates and have troubles setting the value of "FenSelectedValue" in the "FenDropDownListRoles" Control.
I keep getting the error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control in repeater control
But the eval in the Label control works fine. I've been reading here and there, and I read things about it not being bound at the right time so I moved the control from "EditItemTemplate" where it eventually should be to "ItemTemplate", to test it, but still no luck..
<ItemTemplate>
<asp:Label ID="lblRolOmschrijving" Text='<%# Eval("Rol_omschrijving") %>' runat="server" />
<fen:FenDropDownListRoles ID="ddlRoles" FenSelectedValue='<%# Eval("Rol_omschrijving") %>' runat="server" Watermark="AdministratorType" Required="true" ValidationGroup="grpAddUser" />
</ItemTemplate>
Here's how I've learned to set drop down selected items in a grid view.
Example grid:
<div id="gridContainerFormulations">
<script type="text/javascript">
$(document).ready(function () {
//This is done here, instead of codebehind, because the SelectedValue property of the drop down list
//simply does not work when databinding. I set the two 'hid' values via the RowEditing event
$("[id$='drpLotNumber']").val($("#hidSelectedFormulationLotNo").val());
});
</script>
<asp:hiddenfield runat="server" id="hidSelectedFormulationLotNo" value="-1" />
<asp:gridview id="dgrStudyFormulations" cssclass="data" runat="server" allowpaging="False" autogeneratecolumns="False"
datakeynames="Id, FormulationLotNo, FormulationNo">
<Columns>
<asp:BoundField HeaderText="Formulation" ReadOnly="True" DataField="FormulationName" />
<asp:TemplateField HeaderText="Lot #">
<EditItemTemplate>
<asp:dropdownlist ID="drpLotNumber" AddBlank="False" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblLotNumber" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "FormulationLot.Name")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="AI in Formulation" ReadOnly="True" DataField="ActiveIngredientName" />
<asp:TemplateField HeaderText="AI Of Interest">
<EditItemTemplate>
<asp:CheckBox ID="chkOfInterest" Checked='<%# DataBinder.Eval(Container.DataItem, "OfInterest")%>' runat="server" />
</EditItemTemplate>
<ItemTemplate>
<%--<asp:Label ID="lblOfInterest" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "OfInterest")%>' />--%>
<asp:image runat="server" id="imgOfInterest" Visible="False" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="AI Amount" ReadOnly="True" DataField="AIAmountText" />
<asp:CommandField ShowEditButton="True" ShowCancelButton="True" ShowDeleteButton="True"/>
</Columns>
</asp:gridview>
Then in row_editing event of grid:
SelectedFormulationLotNo = CType(dgrStudyFormulations.DataKeys(e.NewEditIndex)("FormulationLotNo"), String)
Which sets the hidden field in the HTML
Property SelectedFormulationLotNo() As String
Get
Return hidSelectedFormulationLotNo.Value.Trim()
End Get
Set(value As String)
If String.IsNullOrEmpty(value) Then
hidSelectedFormulationLotNo.Value = String.Empty
Else
hidSelectedFormulationLotNo.Value = value.Trim()
End If
End Set
End Property
And then the jQuery function call sets the correct option in the newly editable row in the grid.
How I finally did it(but leaving the answer on Rake36's answer, since it probably works too and got me in the direction I needed) Since I couldn't get the Javascript to work for some reason and I knew from messing around that I could get the value of labels in "RowDataBound" I combined the method of Rake36 with the hidden field and set the value in the codebehind (in RowDataBound)
In the codebehind:
protected void gvwUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
DropDownList DropDownListRol = (DropDownList)e.Row.FindControl("ddlRolOmschrijving");
if (e.Row.RowType == DataControlRowType.DataRow && DropDownListRol != null)
{
DsFenVlaanderen.tb_rolDataTable dtRole = DsFenVlaanderen.RolTableAdapter.GetData();
//Fill Dropdownlist
DropDownListRol.DataSource = dtRole;
DropDownListRol.DataValueField = dtRole.Rol_IDColumn.ColumnName;
DropDownListRol.DataTextField = dtRole.Rol_omschrijvingColumn.ColumnName;
DropDownListRol.DataBind();
//Set Selected value
DropDownListRol.Items.FindByValue(hidSelectedRole.Value).Selected = true;
}
}
protected void gvwUsers_RowEditing(object sender, GridViewEditEventArgs e)
{
//Set hiddenfield to value of Rol_ID
hidSelectedRole.Value = gvwUsers.DataKeys[e.NewEditIndex].Values["Rol_ID"].ToString();
}
This is my grid:
<asp:hiddenfield runat="server" id="hidSelectedRole" value="-1" />
<fen:FenGridViewSelectable ID="gvwUsers" runat="server" Selectable="False"
DataSourceID="dsUsers" EnableModelValidation="True" SkinID="Blue"
AllowSorting="True" OnDataBound="gvwUsers_DataBound" OnRowDeleting="gvwUsers_RowDeleting"
AutoGenerateColumns="False" DataKeyNames="User_ID,Rol_ID" OnRowDataBound="gvwUsers_RowDataBound" OnRowEditing="gvwUsers_RowEditing" OnRowUpdating="gvwUsers_RowUpdating">
<Columns>
<asp:BoundField DataField="User_ID" HeaderText="Gebruikersnaam" ReadOnly="True" SortExpression="User_ID" />
<asp:BoundField DataField="User_ID_EXT" HeaderText="Naam" ReadOnly="true" SortExpression="User_ID_EXT" />
<%-- <asp:BoundField DataField="Rol_omschrijving" HeaderText="Type bestuurder" SortExpression="Rol_omschrijving" /> --%>
<asp:TemplateField HeaderText="Type bestuurder" SortExpression="Rol_omschrijving">
<ItemTemplate>
<asp:Label ID="lblRolOmschrijving" Text='<%# Eval("Rol_omschrijving") %>' runat="server"/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlRolOmschrijving" runat="server" DataField="Rol_omschrijving"></asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
<fen:FenTemplateField HeaderStyle-Width="100px">
<ItemTemplate>
<fen:FenButton ID="btnEdit" runat="server" Function="Edit" />
<fen:FenButton ID="btnDelete" runat="server" Function="Delete" />
</ItemTemplate>
<EditItemTemplate>
<fen:FenButton ID="btnUpdate" runat="server" Function="Update" />
<fen:FenButton ID="btnCancel" runat="server" Function="CancelInline" />
</EditItemTemplate>
</fen:FenTemplateField>
</Columns>
</fen:FenGridViewSelectable>
<asp:ObjectDataSource ID="dsUsers" runat="server"
OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
TypeName="FenVlaanderen.DsFenVlaanderenTableAdapters.vUsersTableAdapter"></asp:ObjectDataSource>
<asp:Label ID="lblNoResults" runat="server" Visible="false" CssClass="error">Er werden geen gebruikers gevonden.</asp:Label>
<asp:Label ID="lblDeleteNotAllowed" runat="server" Visible="false" CssClass="error" />
<fen:AddUser ID="addUser" runat="server" OnFenControlSaved="addUser_FenControlSaved" />
</ContentTemplate>

Gridview row.Cells[i].Findcontrol finding control in wrong Cell

I have a function I've written that you pass in the gridview row, as well as what control type you're attempting to locate. It finds the control as intended, however, it finds it in the incorrect cell.
protected int GetCell(GridViewRow row, string strSection)
{
int intVal = 0;
for (int i = 0; i < row.Cells.Count; i++)
{
intVal = i;
if (strSection == "QueryRate")
{
DropDownList cmbQueryType = row.Cells[i].FindControl("cmbQueryType") as DropDownList;
if (cmbQueryType != null)
{
break;
}
}
else if (strSection == "Name")
{
TextBox txtName = row.Cells[i].FindControl("txtName") as TextBox;
if (txtName != null)
{
break;
}
}
}
return intVal;
}
From my understanding, and directly from the Microsoft page: Searches the current naming container for a server control with the specified id parameter.
So my question is, why is this seemingly finding the control is the wrong column? I can't hard code specific columns, because the columns that show vary based on the the datasource.
Per request, the gridview:
<TDWeb:PageGridView SkinID="pagegrid" EmptyDataText="No unmatched parameters." ID="gvUnmatched" runat="server" AllowSorting="True"
OnSorting="gvUnmatched_Sorting" AutoGenerateColumns="False" OnRowDataBound="gvUnmatched_RowDatabound" OnDataBound="gvUnmatched_DataBound"
OnPreRender="gvUnmatched_PreRender" OnPageIndexChanging="OnFilterUnmatched" DataKeyNames="InternalName"
ShowFooter="true" OnPageSizeChanged="gvUnmatched_PageSizeChanged">
<Columns>
<asp:TemplateField runat="server" ItemStyle-CssClass="c" FooterStyle-CssClass="c">
<HeaderTemplate>
<asp:Label runat="server" Text="Add" />
<asp:CheckBox runat="server" ID="ckbAddAll" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="ckbAdd" EnableViewState="true" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" ItemStyle-CssClass="c">
<HeaderTemplate>
<asp:Label runat="server" Text="Ignore" />
<asp:CheckBox runat="server" ID="ckbIgnoreAll" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="ckbIgnore" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" HeaderText="Name" SortExpression="Name" ItemStyle-CssClass="l">
<HeaderTemplate>
<asp:LinkButton runat="server" Text="Name" CommandName="Sort" CommandArgument="Name" />
<td:HelperBox ID="HelperBox5" runat="server" ToolTipText="Set the Name for this parameter. This value must be unique to the CAN Bus or ECU and is the value used throughout the DAP system to reference this parameter." />
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox runat="server" ID="txtName" Width="250" MaxLength="256" style="background-color:inherit" Text='<%# Bind("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" ItemStyle-CssClass="l" SortExpression="Description">
<HeaderTemplate>
<asp:LinkButton runat="server" Text="Description" CommandName="Sort" CommandArgument="Description" />
<td:HelperBox ID="HelperBox4" runat="server" ToolTipText="Set the Description for this parameter. This value is for informational purposes only. Not required." />
</HeaderTemplate>
<ItemTemplate>
<asp:TextBox runat="server" ID="txtDescription" Width="175" MaxLength="500"
TextMode="multiline" style="background-color:inherit" Text='<%# Bind("Description") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" HeaderText="Length (bits)" SortExpression="LengthBits" ItemStyle-CssClass="r">
<ItemTemplate>
<asp:Label runat="server" ID="txtSizeBits" Text='<%# (int)Eval("LengthBits") + (int)Eval("LengthBytes") * 8 %>' style="background-color:inherit" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" AccessibleHeaderText="SourceAddress" HeaderText="SA" SortExpression="Sources" ItemStyle-CssClass="r">
<ItemTemplate>
<asp:Label runat="server" ID="txtSources" Text='<%# Eval("SourceString") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" AccessibleHeaderText="ID" HeaderText="ID/Address" SortExpression="IDorAddress" ItemStyle-CssClass="r">
<ItemTemplate>
<asp:Label runat="server" ID="txtIDorAddress" Text='<%# Eval("IDorAddress") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" HeaderText="Data Type" SortExpression="Datatype" ItemStyle-CssClass="l">
<ItemTemplate>
<asp:Label runat="server" ID="txtDatatype" Text='<%# Bind("Datatype") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" AccessibleHeaderText="QueryRate" ItemStyle-CssClass="r" SortExpression="QueryRate" HeaderText="Query Rate">
<HeaderTemplate>
<asp:Label runat="server" Text="Rate" />
<td:HelperBox ID="HelperBox9" runat="server" ToolTipText="Set the Query Type and Rate for this parameter (if applicable). If set, this value overrides the default Query set below." />
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" ID="lblCANRate" Text='<%# Eval("QueryRateString", "{0} s") %>' Visible='<%# m_SelectedFile.ImportType == App_Code.DAPConfig.ParameterFileImport.EImportType.CANDBC || (m_SelectedFile.ImportType == App_Code.DAPConfig.ParameterFileImport.EImportType.J1939DBC && ((int)Eval("QueryRate")) > 0) %>' />
<asp:DropDownList runat="server" ID="cmbQueryType" style="background-color:inherit" Visible='<%# m_SelectedFile.ImportType != App_Code.DAPConfig.ParameterFileImport.EImportType.CANDBC && (m_SelectedFile.ImportType != App_Code.DAPConfig.ParameterFileImport.EImportType.J1939DBC || (int)Eval("QueryRate") <= 0) %>' />
<asp:Panel runat="server" id="pnlRate" style="display:none" ><asp:TextBox runat="server" ID="txtRate" Width="50" MaxLength="6" style="background-color:inherit" />s</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField runat="server" HeaderText="Category" ItemStyle-CssClass="l">
<HeaderTemplate>
<asp:Label runat="server" Text="Category" />
<td:HelperBox ID="HelperBox10" runat="server" ToolTipText="Set the Category with which to group this parameter, or create a new category." />
</HeaderTemplate>
<ItemTemplate>
<asp:DropDownList runat="server" ID="cmbCategory" style="background-color:inherit" >
<asp:ListItem Value="_New_" Text="Add New..." />
</asp:DropDownList>
<br />
<asp:TextBox Width="100" MaxLength="50" runat="server" ID="txtCategory" style="display:none;background-color:inherit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</TDWeb:PageGridView>
ok - turns out that calling row.cells(0).FindControl("") is the equivalent of calling row.FindControl("") because FindControl() operates within the current NamingContainer. And all the cells in a given row have the same NamingContainer, specifically {System.Web.UI.WebControls.GridViewRow}
If all you need is to determine the column index of a given control try this (pardon the VB):
Public Function getCellIndexByName(ByVal row As GridViewRow, ByVal ColumnName As String) As Integer
For ci As Integer = 0 To row.Cells.Count - 1
If CType(row.Cells(ci), DataControlFieldCell).ContainingField.ToString() = ColumnName Then
Return ci
End If
Next
Return -1
End Function
If you know the ID of the control that you are trying to find, you don't need to specify the cell. You can find it by looking in the whole row.
DropDownList cmbQueryType = row.FindControl("cmbQueryType") as DropDownList
This way you avoid the problem of incorrect cell index.

Asp.Net GridView Sorting Separate Page

I have an issue with having a asp.net Grid View loaded into a div tag on a page (UI/Host.aspx). The Grid View itself is on a seperate page (GridViews/GridView1.aspx) and I'm using the jQuery load() function to load this page into the div tag.
My problem is when sorting the grid view it tries to postback to the page that's hosting it, and comes back with the error "Unable to find page UI/GridView1.aspx", is there a way to override this so that it post backs to itself, (which I assumed it would but doesn't) or is there an easier way to do the sorting.
Is there any other way of doing this, even if it means getting rid of the GridView altogether and using a repeater and table?
Below is the code:
UI/Hosts.aspx
//jQuery to load the div with the page UI/Hosts.aspx
$(document).ready(function() {
StartRefresh();
});
function startRefresh()
{
refreshID = setInterval(function() {
DisplayDate();
$("#divDests").load("../GridViews/Gridview1.aspx?ConfigID=" + $("#ctl00_MainContent_dlConfiguration").val() + "&ModuleID=" + $("#ctl00_MainContent_ddlModule").val());
}, $("#ctl00_MainContent_ddlRefresh :selected").val());
}
GridViews/Gridview1.aspx;
//Markup for GridViews/Gridview1.aspx
<html>
<head><title></title></head>
<body>
<form runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<br />
<asp:GridView Font-Size="8.7pt" ID="gvLiveDest" runat="server" AutoGenerateColumns="False"
EmptyDataText="No Records Found" AllowSorting="true"
onsorting="gvLiveDest_Sorting" onrowcreated="gvLiveDest_RowCreated" OnRowDataBound="gvLiveDest_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Name" SortExpression="DestinationName" HeaderStyle-CssClass="centralalignment">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# WebHelper.HTMLSafe(Eval("Description")) %>' ToolTip='<%# WebHelper.HTMLSafe(Eval("Description")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Logged <br /> In" HeaderStyle-CssClass="centralalignment" SortExpression="LoggedIn" >
<ItemStyle CssClass="centralalignment" />
<ItemTemplate>
<asp:Label ID="lblLoggedIn" runat="server" Text='<%# SetLoggedIn(Convert.ToBoolean(Eval("Active"))) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Current<br />Status" HeaderStyle-CssClass="centralalignment" SortExpression="LastStatus" >
<ItemStyle CssClass="centralalignment" />
<ItemTemplate>
<asp:Label ID="lblCurrentStatus" runat="server" Text='<%# WebHelper.HTMLSafe(Eval("LastStatus")) %>' ToolTip='<%#Eval("LastStatus") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Time in<br />Current<br />Status" HeaderStyle-CssClass="centralalignment" SortExpression="CurrentDuration">
<ItemStyle CssClass="RightAlignment" />
<ItemTemplate>
<asp:Label ID="lblCurrentTime" runat="server" Text='<%# ICT.DAL.Reporting.CallDurFormat(Eval("CurrentDuration")) %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Lines" HeaderText="Lines" HeaderStyle-CssClass="centralalignment" SortExpression="Lines"
ItemStyle-CssClass="centralalignment" />
<asp:BoundField DataField="LinesBusy" HeaderText="Lines <br /> Busy" HeaderStyle-CssClass="centralalignment"
ItemStyle-CssClass="centralalignment" ReadOnly="True" HtmlEncode="False" SortExpression="LinesBusy" />
<asp:BoundField DataField="LinesAvailable" HeaderStyle-CssClass="centralalignment"
ItemStyle-CssClass="centralalignment" SortExpression="LinesAvailable"
HeaderText="Lines <br /> Available" HtmlEncode="false" ReadOnly="True" />
<asp:TemplateField HeaderText="Last Call Time" SortExpression="Timestamp" HeaderStyle-CssClass="centralalignment">
<ItemTemplate>
<asp:Label ID="lblLastCallTime" runat="server" Text='<%# WebHelper.HTMLSafe(Eval("LastCallTime")) %>' ToolTip='<%# WebHelper.HTMLSafe(Eval("LastCallTime")) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
And the onSort Event Code (However it never hits this)
protected void gvLiveDest_Sorting(object sender, GridViewSortEventArgs e)
{
if (string.Compare(e.SortExpression, ViewState["SortField"].ToString(), true) == 0)
{
_sortDir = (_sortDir == "ASC") ? "DESC" : "ASC";
}
else
_sortDir = "ASC";
_SortField = e.SortExpression;
ViewState["SortField"] = e.SortExpression;
ViewState["sortDir"] = _sortDir;
BindLiveDestination();
}
I switched over to client-side paging/sorting a while ago and haven't been happier. Of course, you would need to set AllowSorting="false" and AllowPaging="false" in your GridView.
You could put it into an iframe...

DetailsView change text to a label on ItemInserted event

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%

Categories