I get this error on this column in a `gridview' :-
<asp:TemplateField>
<ItemTemplate>
<%# Eval("title") %>
<br />
<p runat="server" id="description"><%# Eval("des") %></p>
<asp:ImageButton ID="click_like" runat="server" ImageUrl= "~/pics/like.png" Height="20px" CommandName="like" CommandArgument='<%# Eval("id") %>' BorderStyle="Outset" BorderColor="#99CCFF" Enabled='<%# Eval("like_enabled") %>' />
<asp:Label ID="lbl_like" runat="server" Text='<%# Eval("like") %>'></asp:Label>
<asp:ImageButton ID="click_dislike" runat="server" ImageUrl="~/pics/dislike.png" Height="20px" CommandName="dislike" CommandArgument='<%# Eval("id") %>' BorderStyle="Outset" BorderColor="#99CCFF" Enabled='<%# Eval("dislike_enabled") %>'/>
<asp:Label ID="lbl_dislike" runat="server" Text='<%# Eval("dislike") %>'></asp:Label>
<p runat="server" id="labels"><%# Eval("labels") %></p>
</ItemTemplate>
</asp:TemplateField>
I get this error on the line:-
<asp:ImageButton ID="click_like" runat="server" ImageUrl= "~/pics/like.png" Height="20px" CommandName="like" CommandArgument='<%# Eval("id") %>' BorderStyle="Outset" BorderColor="#99CCFF" Enabled='<%# Eval("like_enabled") %>' />
And I get this because I am binding the value of Enabled attribute from a DataTable where like_enabled column is either "true" or "false"
You're not able to bind Enabled like that, you'll have to use an RowDataBound event:
<asp:GridView id="myGrid" OnRowDataBound="myGrid_RowDatabound">
Code behind:
public void myGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var enabled = (bool)DataBinder.Eval(e.Row.DataItem, "like_enabled");
var click_like = e.Row.FindControl("click_like") as ImageButton;
click_like.Enabled = enabled;
}
}
Related
My dataset return this table link.How can i use If else conditons in aspx page?I want if the File_path is Null or empty then image not show because if File_Path is empty then it give me error,I want if File_Path is not empty then image will be show.Following error
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Here is my code
<asp:DataList ID="DataList2" runat="server" Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" HorizontalAlign="Center" RepeatLayout="Table">
<ItemTemplate>
<div class="<%#GetStyleForMsgList(Eval("MsgSender").ToString()) %> MainChatListClass">
<asp:Label ID="Label1" runat="server" Text='<%# GetPerfactName(Eval("MsgSender").ToString()) %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("ChatMsg") %>'></asp:Label>
<asp:Label ID="lblfile" runat="server" Visible ="false" Text='<%# Eval("File_Path") %>'></asp:Label>
<% if (Eval("File_Path") != null)
{%>
<asp:Image ID ="img" runat ="server" ImageUrl ='<%# Eval("File_Path") %>' />
<% } %>
</div>
</ItemTemplate>
</asp:DataList>
This is my GridView:
<asp:GridView ID="GridView1" runat="server" BorderColor="#ececec" BorderWidth="2px" HeaderStyle-BackColor="#F6F6F6" HeaderStyle-Font-Bold="true" RowStyle-BackColor="white" AlternatingRowStyle-BackColor="#F6F6F6" AutoGenerateColumns="False" ShowFooter="true" DataKeyNames="UsuarioId" ShowHeaderWhenEmpty="True" OnRowCommand="GridView1_OnRowCommand" OnRowEditing="GridView1_OnRowEditing" OnRowCancelingEdit="GridView1_OnRowCancelingEdit" OnRowUpdating="GridView1_OnRowUpdating" OnRowDeleting="GridView1_OnRowDeleting" Width="100%" CssClass="GridViewStyle" >
<EmptyDataTemplate>
<div>
No hay datos para mostrar :(
</div>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="Tipo Usuario ID (Dato momentaneo)">
<ItemTemplate>
<asp:Label Text='<%#Eval("tipo") %>' runat="server">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTipoUsuarioId" Text='<%#Eval("tipo") %>' runat="server" ></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="dropdown" runat="server" AppendDataBoundItems="True" DataTextField="tipoUsuario" DataValueField="tipoUsuarioId" DataSourceID="sqlDB" CssClass="ddl">
</asp:DropDownList>
<asp:SqlDataSource ID="sqlDB" ConnectionString="<%$ ConnectionStrings:conn%>" runat="server" SelectCommand="Select * from Tipo_Usuario"></asp:SqlDataSource>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Nombre">
<ItemTemplate>
<asp:Label Text='<%#Eval("nombre") %>' runat="server">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtNombre" Text='<%#Eval("nombre") %>' runat="server" required="true"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNombreFooter" runat="server"></asp:TextBox>
<br/>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator1" ForeColor="Red" runat="server" ValidationGroup="Group1" ControlToValidate="txtNombreFooter"
ErrorMessage="Requiere nombre!"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contraseña">
<ItemTemplate>
<asp:Label Text='<%#Eval("contraseña") %>' runat="server" >
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtContraseña" Text='<%#Eval("contraseña") %>' runat="server" required="true" ></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtContraseñaFooter" runat="server" ></asp:TextBox>
<br/>
<asp:RequiredFieldValidator
ID="RequiredFieldValidator2" ForeColor="Red" runat="server" ValidationGroup="Group1" ControlToValidate="txtContraseñaFooter"
ErrorMessage="Requiere contraseña!"></asp:RequiredFieldValidator>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Habilitado">
<ItemTemplate>
<asp:Label id="habilitado" Text='<%#Eval("habilitado") %>' runat="server">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtHabilitado" Text='<%#Eval("habilitado") %>' runat="server" ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton CssClass="btn btn-warning" BackColor="#EC971F" Text="Editar" ForeColor="white" CommandName="Edit" runat="server" ><i class="fa fa-pencil" aria-hidden="true"></i></asp:LinkButton>
<asp:LinkButton CssClass="btn btn-danger" Text="Borrar" CommandName="Delete" runat="server" > <i class="fa fa-trash-o" aria-hidden="true"></i></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ImageUrl="Assets/ICONS/Save.png" CommandName="Update" ToolTip="Update" Width="20" Height="20" runat="server"/>
<asp:ImageButton ImageUrl="Assets/ICONS/Cancel.png" CommandName="Cancel" ToolTip="Cancel" Width="20" Height="20" runat="server"/>
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ImageUrl="Assets/ICONS/xls.png" OnClick="ExportDataToExcel" Width="40" Height="40" runat="server"/>
<asp:LinkButton CssClass="btn btn-success" Text="Agregar" CommandName="Agregar" runat="server" ValidationGroup="Group1"></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is the code to export:
public void ExportDataToExcel(object sender, EventArgs e)
{
GridView1.AllowPaging = false;
GridView1.AllowSorting = false;
GridView1.AutoGenerateEditButton = false;
GridView1.AutoGenerateDeleteButton = false;
Response.ClearContent(); //Clear any content
Response.AppendHeader("content-disposition","attachment; filename=Usuarios.xls"); //Export as XLS (Excel)
Response.ContentType = "application/excel";
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter); //Saves all the content including rows.
GridView1.RenderControl(htmlTextWriter);
Response.Write(stringWriter.ToString()); //Gridview data is going to be converted for a xls file
Response.End();
}
This is what I get:
Like I said on the title, I want to hide TextBox and buttons while exporting to excel in order to only see the data. Those controls are inside EditItemTemplate tags. On the aspx website the GridView show data from SQL server database, what I want to do is to export it as a table.
I'm trying to add a data-th in a gridView but I don't know how, this attribute is added in a td
Something like this
<tr>
<td data-th="Movie Title">Star Wars</td>
<td data-th="Genre">Adventure, Sci-fi</td>
<td data-th="Year">1977</td>
<td data-th="Gross">$460,935,665</td></tr>
This is my asp.net code
I tried add data-th in the item template and the Templatefield but it isn't working
<asp:GridView ID="CerrTick" AllowPaging="true" PageSize="15" Width="100%" class="rwd-table" OnPageIndexChanging="CerrTick_PageIndexChanging" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="CerrTick_SelectedIndexChanged" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnC" CommandName="Select" class="button prefix bom" runat="server" Text="Seleccionar" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="N° ticket">
<ItemTemplate >
<asp:Label ID="N_Tick" runat="server" Text='<%# Eval("N° ticket") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tecnico">
<ItemTemplate>
<asp:Label ID="Tec" runat="server" Text='<%# Eval("Tecnico") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Empresa">
<ItemTemplate>
<asp:Label ID="Emp" runat="server" Text='<%# Eval("empresa") %>'></asp:Label>
<asp:Label ID="id_ticket" runat="server" Text='<%# Eval("id_ticket") %>' Visible="false"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Servicio">
<ItemTemplate>
<asp:Label ID="serv" runat="server" Text='<%# Eval("servicio") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Use Code behind RowDataBound
protected void CerrTick_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TableCellCollection cell = e.Row.Cells;
cell[0].Attributes.Add("data-th", "Movie Title");
cell[1].Attributes.Add("data-th", "Genre");
cell[2].Attributes.Add("data-th", "Year");
cell[3].Attributes.Add("data-th", "Gross");
}
}
If you want to avoid doing it in the code behind, you could build the table using a ListView or Repeater and dynamically assign the data attribute. I'm doing something similar, although the data attribute is assigned to the label inside the gridview rather than the cell itself.
<asp:Label ID="Label1" runat="server" Text='<%#Eval("value") %>' data-frequency='<%#Eval("frequency") %>' />
When the user changes the text in the textbox's in the edit template and clicks update, when I try to grab those new values it still is graving the old value of the text box.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="CompanyID" CellPadding="4"
GridLines="None" Width="1079px" ForeColor="#333333"
OnRowCancelingEdit="GridView1_RowCancelling"
OnRowUpdating="GridView1_RowUpdating"
OnRowEditing="GridView1_RowEditing">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" CommandArgument='<%# Eval("CompanyID") %>' Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Issue Date">
<ItemTemplate>
<asp:Label runat="server" ID="IssueDate" Text='<%#Eval("IssueDate") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtIssueDate" Text='<%#Eval("IssueDate") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Notice Intent Response Due">
<ItemTemplate>
<asp:Label runat="server" ID="NoticeIntentResponseDue" Text='<%#Eval("NoticeIntentResponseDue") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtNoticeIntentResponseDue" Text='<%#Eval("NoticeIntentResponseDue") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Deadline For Questions">
<ItemTemplate>
<asp:Label runat="server" ID="DeadlineForQuestions" Text='<%#Eval("DeadlineForQuestions") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtDeadlineForQuestions" Text='<%#Eval("DeadlineForQuestions") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bids Due">
<ItemTemplate>
<asp:Label runat="server" ID="BidsDue" Text='<%#Eval("BidsDue") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtBidsDue" Text='<%#Eval("BidsDue") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Shortlist Notice">
<ItemTemplate>
<asp:Label runat="server" ID="ShortlistNotice" Text='<%#Eval("ShortlistNotice") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtShortlistNotice" Text='<%#Eval("ShortlistNotice") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Final Selection">
<ItemTemplate>
<asp:Label runat="server" ID="FinalSelection" Text='<%#Eval("FinalSelection") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtFinalSelection" Text='<%#Eval("FinalSelection") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false" HeaderText="CompanyID">
<ItemTemplate>
<asp:Label runat="server" ID="CompanyID" Text='<%#Eval("CompanyID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The update button calls this function:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int key = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
Label CompanyID = (Label)GridView1.Rows[e.RowIndex].FindControl("txtCompanyID");
TextBox thisIssueDate = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtIssueDate"));
TextBox NoticeIntentResponseDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNoticeIntentResponseDue");
Response.Write(NoticeIntentResponseDue.Text + " " + thisIssueDate.Text);
Response.End();
TextBox DeadlineForQuestions = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDeadlineForQuestions");
TextBox BidsDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBidsDue");
TextBox ShortlistNotice = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtShortlistNotice");
TextBox FinalSelection = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFinalSelection");
}
The response is showing me that the value being grabbed is still the origonal text value of the box. Not what you typed into the box.
In your grid view row updating event add the following condition
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit )
{
int key = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);
Label CompanyID = (Label)GridView1.Rows[e.RowIndex].FindControl("txtCompanyID");
TextBox thisIssueDate = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtIssueDate"));
TextBox NoticeIntentResponseDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtNoticeIntentResponseDue");
Response.Write(NoticeIntentResponseDue.Text + " " + thisIssueDate.Text);
Response.End();
TextBox DeadlineForQuestions = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtDeadlineForQuestions");
TextBox BidsDue = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtBidsDue");
TextBox ShortlistNotice = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtShortlistNotice");
TextBox FinalSelection = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFinalSelection");
}
}
Update:
the problem looks like that you have also bind your Edit Item template columns with the data from data table, and when you are getting the data in the code behind you are not getting the updated data which the user updates in edit mode and u still getting the old data. If you remove the Binding from the Edit Item Template feilds then your code will work.
I figured it out, Derek was right. It had to do with the Binding Data on postback in page load. I binded the data every time instead of just the first time. Thanks
This is my grid
<asp:GridView ID="gridProduct" runat="server"
AutoGenerateColumns="false"
ShowFooter="true"
onrowcancelingedit="gridProduct_RowCancelingEdit"
onrowdeleting="gridProduct_RowDeleting" onrowediting="gridProduct_RowEditing"
onrowupdating="gridProduct_RowUpdating"
onrowcommand="gridProduct_RowCommand"
onrowdatabound="gridProduct_RowDataBound">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:Button ID="buttonUpdate" CommandName="Update" runat="server" ToolTip="Update" Text="Update" />
<asp:Button ID="buttonCancel" CommandName="Cancel" runat="server" ToolTip="Cancel" Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="buttonEdit" CommandName="Edit" runat="server" Text="Edit" ToolTip="Edit"/>
<asp:Button ID="buttonDelete" CommandName="Delete" runat="server" Text="Delete" ToolTip="Delete"/>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="buttonAdd" runat="server" Text="Ajouter" CommandName="AddNew" ToolTip="Add new User" ValidationGroup="validaiton" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="#Piece">
<EditItemTemplate>
<asp:Label ID="labelEditPiece" runat="server" Text='<%#Eval("Piece") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labelItemPiece" runat="server" Text='<%#Eval("Piece") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="dropDownListPartsFooter" runat="server" DataTextField="Nom" DataValueField="ID_AchatTemplate">
</asp:DropDownList>
ControlToValidate="txtBoxPiece" Text="*" ValidationGroup="validaiton"/>--%>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Series">
<EditItemTemplate>
<asp:Label ID="labelEditSeries" runat="server" Text='<%#Eval("Series") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="labelItemSeries" runat="server" Text='<%#Eval("Series") %>'/>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtBoxSeries" runat="server"/>
<asp:RequiredFieldValidator ID="fieldValidSeries" runat="server" ControlToValidate="txtBoxSeries" Text="*" ValidationGroup="validaiton"/>
</FooterTemplate>
.... </asp:TemplateField>
</Columns>
This is my page load
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PsaDataSet psaList = new PsaDataSet();
ViewState.Remove("psaList");
ViewState.Add("psaList", psaList);
ViewState.Add("psaUid", Guid.NewGuid());
if (psaList.PsaLink.DefaultView.Count == 0)
{
// Patch for view footer row when no data
PsaDataSet.PsaLinkDataTable tmpList = new PsaDataSet.PsaLinkDataTable();
PsaDataSet.PsaLinkRow tmpItem = tmpList.NewPsaLinkRow();
tmpItem.PsaUid = (Guid)ViewState["psaUid"];
tmpItem.PsaProductUid = Guid.Empty;
tmpItem.ProductId = 1;
tmpItem.Series = "test";
tmpItem.Rev = "test";
tmpItem.Firmware = "test";
tmpList.AddPsaLinkRow(tmpItem);
tmpList.AcceptChanges();
ViewState.Add("series", tmpItem.Series);
gridProduct.DataSource = tmpList;
gridProduct.DataBind();
}
}
else
{
//BindGrid((PsaDataSet)ViewState["psaList"], false);
}
}
private void BindGrid(PsaDataSet psaList, bool mustDataBind)
{
gridProduct.DataSource = psaList.PsaLink;
//if (mustDataBind)
//{
gridProduct.DataBind();
//}
}
This my onrowdatabound="gridProduct_RowDataBound"> method
protected void gridProduct_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["style"] = "display:none";
}
}
}
I want to add a condition (Based whit a test value inserted in page load) in the if(protected void gridProduct_RowDataBound method) for hiding just one time on page load??
Thank Frank!
I belive you can get the DataBoundItem from the row which is a type of "PsaDataSet.PsaLinkRow" and use that you get the ProductId, Series, etc. and do the condition that you require. Also, you have and if condition inside an if condition with the same condition for both. You only need one.