I have some ASP that I want to look kinda of like this:
<asp:DataGrid ID="dgEnum" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="<%# some big DataBinder expression %>" runat="server" />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
but that gives me a:
Parser Error Message: The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: <asp:Button runat="server" id="Button1" />
Anyone have an idea how to hack around that?
Anyone have an idea how to hack around that?
You can't, and you don't. You can store the required data somewhere besides the ID. At the very least, a sibling HiddenField could be used.
<script runat="server">
void Checkbox_CheckedChanged(object sender, EventArgs e) {
var c = (Checkbox)sender;
if (!c.Checked) {
return;
}
var hfv = (HiddenField)c.Parent.FindControl("hfvMyData");
var myData = hfv.Value;
/* Do something with myData */
}
</script>
<asp:DataGrid ID="dgEnum" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox runat="server" OnCheckedChanged="Checkbox_CheckedChanged" />
<asp:HiddenField id="hfvMyData" runat="server" Value='<%# Eval("MyData") %>' />
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
Other options could be DataKeys, a server side (perhaps cached or Session data) indexed list, or a ViewState indexed list.
If you really wanted to bastardize WebForms, you can put your data into CssClass...but that's just crazy. ;)
Not sure what you are intending to do here, but you can just go straight to the html controls ie <input type="checkbox" id="<%# some expresssion %>" />
Or you don't do this and query for it on the server side. I.e. find the row you want and then use FindControls() to get the checkbox for that row.
Related
My asp:GridView has the options to edit / delete rows. I would like those two options to be in the same cell of every row that I have (currently these two options are presented in different cells).
This is the code that I have right now:
<asp:CommandField ShowEditButton='True' edittext='edit' canceltext='cancel' updatetext='update'></asp:CommandField>
<asp:CommandField ShowDeleteButton='True' DeleteText='delete' ></asp:CommandField>
Any help will be appreciated!!!
Try asp:TemplateField
and put both of your options there? I'm not sure if that will work. But have you tried it?
Try using a template column like this:
<asp:GridView ID="grid" runat="server" OnSelectedIndexChanged="grid_SelectedIndexChanged">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button id="btnEdit" runat="server" />
<asp:Button id="btnDelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Then you'll have to add the code for your grid_SelectedIndexChanged
protected void grid_SelectedIndexChanged(object sender, EventArgs e)
{
}
I have a Details View that has a file upload field in it. When I fill out the information and upload a file (I have tried multiple files ranging from 9k to 6.8MB) all of the information (text fields) submit fine, but the uploaded file is always returning a false when I check the HasFile and always returns String.Empty when I check the file name.
Am I doing something wrong? The details view is in a Panel and Not an Update Panel
<asp:Panel ID="pnlUpdate" runat="server"
Visible="false">
<h4 runat="server" id="h2SubCaption">Person Details</h4>
<asp:DetailsView ID="dvAssignment"
runat="server"
AutoGenerateRows="false"
Width="100%"
SkinID="SampleDetailsView"
CssSelectorClass="PrettyDetailsView"
DataKeyNames="guidMemberId"
DefaultMode="Edit"
OnItemUpdating="dvAssignment_ItemUpdating"
OnModeChanging="dvAssignment_ModeChanging"
AutoGenerateEditButton="True" >
<Fields>
<asp:TemplateField HeaderText="Nomination Letter">
<EditItemTemplate>
<asp:FileUpload runat="server" ID="fileuploadNomination" />
</EditItemTemplate>
</asp:TemplateField> .....
Code Behind:
FileUpload _nomination = (FileUpload)dv.FindControl("fileuploadNomination");
byte[] nominationByte = null;
if (_nomination.FileName != string.Empty)
nominationByte = _nomination.FileBytes;
//if(_nomination.HasFile)
//nominationByte = _nomination.FileBytes;
EDIT
I added a Page_Load call and it looks as if the page is posting back when I click the Auto Generated Update Button for the DetailsView. This postback is probably clearing out my FileUpload field. Any ideas on how to get around it?
Edit #2 I have now put an update panel around the DetailsView and set the postback trigger the DetailsView (see below) and it still is not working, it seems to be clearing the upload control prior to submitting.
<asp:UpdatePanel ID="updatePnl" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlUpdate" runat="server"
Visible="false">
<h4 runat="server" id="h2SubCaption">Person Details</h4>
<asp:DetailsView ID="dvAssignment"
runat="server"
AutoGenerateRows="false"
Width="100%"
SkinID="SampleDetailsView"
CssSelectorClass="PrettyDetailsView"
DataKeyNames="guidMemberId"
DefaultMode="Edit"
OnItemUpdating="dvAssignment_ItemUpdating"
OnModeChanging="dvAssignment_ModeChanging"
AutoGenerateEditButton="True" >
<FieldHeaderStyle Font-Bold="True" Width="150px" />
<Fields>
<asp:FileUpload runat="server" ID="fileuploadNomination" />
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView >
</asp:Panel>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="dvAssignment" />
</Triggers>
</asp:UpdatePanel>
Gridview Code as requested
<asp:GridView ID="gvQuality"
runat="server"
AutoGenerateColumns="False"
Width="100%"
DataKeyNames="guidMemberId"
CssSelectorClass="PrettyGridView"
SkinID="SampleGridView"
OnSelectedIndexChanged="gvQuality_SelectedIndexChanged"
onrowdatabound="gvQuality_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnViewDetails" runat="server" Text="Edit" CommandName="Select" />
</ItemTemplate>
</asp:TemplateField>
a few bound fields are after this (first name, last name, etc)
protected void gvQuality_SelectedIndexChanged(object sender, EventArgs e)
{
Guid guidMemberId = (Guid)gvQuality.SelectedDataKey.Values["guidMemberId"];
PortalDataContext db = new PortalDataContext(AuthenticatedUser.ConnectionString);
h2SubCaption.InnerText = "Update Person";
dvAssignment.ChangeMode(DetailsViewMode.Edit);
dvAssignment.DataSource = LINQ Query Here
dvAssignment.DataBind();
}
Everyone, Thanks for all of the help but I figured it out. I had to set the Page.Form.Enctype = "multipart/form-data.
Here is the code for it to work!
protected void Page_Load(object sender, EventArgs e)
{
this.Page.Form.Enctype = "multipart/form-data";
}
I'm sorry if this is a dumb answer but I usually use on gridviews the selectedindexchanging to capture current row values. Have you tried that instead of the selectedindexchanged?
you can try
Request.Files[0] get the upload file
Make sure you only have one <form> on the same page.
The gridview control makes databinding of complex objects rather easy. In my scenario a gridview control gets bound to a Customer object which has a few 'flat' properties and one complex property that takes an object of the type Address. The grid displays the data as expected. The problem is that I have found no way to access the values of the Address properties in code behind. For example, setting the DataKeyNames collection to DataKeyNames="Id, Address.Id" results in an error:
DataBinding: System.Data.Entity.DynamicProxies.Customer_95531162E60920A5C3C02043F6564873913B91785C856624301E8B6E89906BF6 does not contain a property with the name Address.Id.
What is the proper way to access the value of the Address.Id field in code behind? Ideally I'd like to do something like:
protected void CustomerDetailsObjectDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
if (CustomersGridView.SelectedIndex < 0) return;
// Retrieving the Customer's id works:
e.InputParameters["id"] = Convert.ToString(CustomersGridView.DataKeys[CustomersGridView.SelectedIndex].Value);
// Retrieving the Address id doesn work:
e.InputParameters["id"] = Convert.ToString(CustomersGridView.DataKeys[CustomersGridView.SelectedIndex].Values["Address.Id"].ToString());
}
Here's the asp code:
<ContentTemplate>
<asp:GridView ID="CustomersGridView" runat="server" AutoGenerateColumns="False" DataSourceID="CustomersObjectDataSource"
onselectedindexchanged="CustomersGridView_SelectedIndexChanged" DataKeyNames="Id,Address.Id" ondatabound="CustomersGridView_DataBound">
<Columns>
<asp:TemplateField HeaderText="Aktion">
<ItemTemplate>
<asp:LinkButton runat="server" ID="SelectCustomerButton" Text="Auswählen" CommandName="Select" /> <br/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Kunde" SortExpression="LastName" ItemStyle-VerticalAlign="Top" >
<ItemTemplate>
<asp:Label ID="NumberLabel" runat="server" Text='<%#"GpNr: " + Eval("Number")%>'></asp:Label><br/>
<asp:Label ID="SalutationLabel" runat="server" Text='<%#Eval("Salutation")%>'></asp:Label>
<asp:Label ID="TitleLabel" runat="server" Text='<%#Eval("Title")%>'></asp:Label>
<asp:Label ID="FirstNameLabel" runat="server" Text='<%#Eval("FirstName")%>'></asp:Label>
<asp:Label ID="LastNameLabel" runat="server" Text='<%#Eval("LastName")%>'></asp:Label><br/>
<asp:Label ID="NameContactPersonLabel" runat="server" Text='<%#"Kontakt: " + Eval("NameContactPerson")%>'></asp:Label>
</ItemTemplate>
<ItemStyle VerticalAlign="Top" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Adresse" SortExpression="Address.PostalCode" ItemStyle-VerticalAlign="Top" >
<ItemTemplate>
<asp:Label ID="AddressIdLabel" runat="server" Text = '<%#Eval("Address.Id") %>'></asp:Label>
<asp:Label ID="AddressStreetLabel" runat="server" Text='<%#Eval("Address.Street")%>'></asp:Label>
<asp:Label ID="AddressHouseNumberLabel" runat="server" Text='<%#Eval("Address.HouseNumber")%>'></asp:Label>
<asp:Label ID="AddressHouseNumberExtensionLabel" runat="server" Text='<%#Eval("Address.HouseNumberExtension")%>'></asp:Label>
<asp:Label ID="AddressDoorNumberLabel" runat="server" Text='<%#Eval("Address.DoorNumber")%>'></asp:Label><br/>
<asp:Label ID="AddressPostalCodeLabel" runat="server" Text='<%#Eval("Address.PostalCode")%>'></asp:Label>
<asp:Label ID="AddressCityLabel" runat="server" Text='<%#Eval("Address.City")%>'></asp:Label><br/>
<asp:Label ID="AddressCountryLabel" runat="server" Text='<%#Eval("Address.Country")%>'></asp:Label>
</ItemTemplate>
Thanks!!
the reason is Gridview consider datakeynames as the property name, it is using DataBinder.GetPropertyValue() during CreateChildControls() to retrieve data key values, while DataBinder.Eval support multiple level property( as long as it is separated by dot character)
Example:
object item = ...
DataBinder.Eval(item, "Address.Id"); //no problem
DataBinder.GetPropertyValue(item, "Address.Id"); //will throw exception
there are several solution to solve your problem:
derive your custom gridview(override the CreateChildControls()) that it's datakeynames support multi layer expression. You may refer the default implementation by using Reflector. (lots of work)
store the complex property values into viewstate during RowDataBound event (prefered, and also used by me too :) )
Example:
private void CustomersGridView_RowDataBound(object sender, EventArgs args)
{
if(e.Row.RowType = DataControlRowType.DataRow)
{
object id = DataBinder.Eval(e.Row.DataItem, "Id");
//DictionaryInViewState is a variable that will be stored into viewstate later
DictionaryInViewState[id] = DataBinder.Eval(e.Row.DataItem, "Address.Id");
}
}
References:
http://www.telerik.com/forums/problem-with-a-complex-datakeynames-value
I would like the rows of my GridView to have strikethrough based on a bound data value called IsObsolete. I tried to do this:
<RowStyle BackColor="#EFF3FB" Font-Strikeout='<%# Bind('IsObsolete') %>' />
But obviously this doesn't parse. I'd rather not do this in GridView.DataBound(). Any other ideas?
I do this by applying a style on the DataBinding event of one of my controls in a template. Example:
<asp:GridView ID="grdYourGrid" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="SomeTitle">
<ItemTemplate>
<asp:HyperLink ID="hrefYourLink" runat="server"
NavigateUrl="Somepage.aspx?id={0}"
OnDataBinding="hrefYourLink_DataBinding"></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Then implement the OnDataBinding event:
protected void hrefYourLink_DataBinding(object sender, System.EventArgs e)
{
HyperLink link = (HyperLink)(sender);
GridViewRow row = (GridViewRow)(link.Parent.Parent);
if ((bool)(Eval("IsObsolete"))
{
row.CssClass = "StrikeThroughStyle";
}
link.Text = HttpUtility.HtmlEncode(((int)(Eval("ID"))).ToString());
link.NavigateUrl = string.Format(link.NavigateUrl, Eval("ID").ToString());
}
This is just an quick example with a column with a link that gets modified based on the databinding as well but you should be able to get the gist of if an tweak it to suit your needs. I like doing it on the databinding because I do no binding inline in my aspx code.
Since the RowStyle element is applicable to the entire grid, the only way to accomplish what you want would be to have TemplateItems set for all columns and apply a CssClass to each column based on that same data value.
I'm not sure for your reasoning for avoiding the DataBound event for doing this as that would be the simplest way to accomplish it.
You might also try using a formatting function and itemstyles. Stealing a tidbit of code from above and changing it:
<%
public string GetObsoleteClass(string obsolete)
{
bool obs = Convert.ToBoolean(obsolete);
obs ? return "myObsoleteClass" : return "myNotObsoleteClass";
}
%>
<asp:GridView ID="grdYourGrid" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="SomeTitle">
<ItemTemplate>
<asp:HyperLink ID="hrefYourLink" runat="server"
NavigateUrl="Somepage.aspx?id={0}"
OnDataBinding="hrefYourLink_DataBinding"></asp:HyperLink>
</ItemTemplate>
<itemstyle CssClass='<%# Eval("isObsolete") %>'>
</itemstyle>
</asp:TemplateField>
<asp:boundfield
sortexpression="LastName"
datafield="LastName"
headertext="LastName">
<itemstyle CssClass='<%# Eval("isObsolete") %>'>
</itemstyle>
</asp:boundfield>
</Columns>
</asp:GridView>
Here's my code in a gridview that is bound at runtime:
...
<asp:templatefield>
<edititemtemplate>
<asp:dropdownlist runat="server" id="ddgvOpp" />
</edititemtemplate>
<itemtemplate>
<%# Eval("opponent.name") %>
</itemtemplate>
</asp:templatefield>
...
I want to bind the dropdownlist "ddgvOpp" but i don't know how. I should, but I don't. Here's what I have, but I keep getting an "Object reference" error, which makes sense:
protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) //skip header row
{
DropDownList ddOpp = (DropDownList)e.Row.Cells[5].FindControl("ddgvOpp");
BindOpponentDD(ddOpp);
}
}
Where BindOpponentDD() is just where the DropDownList gets populated. Am I not doing this in the right event? If not, which do I need to put it in?
Thanks so much in advance...
Ok, I guess I'm just dumb. I figured it out.
In the RowDataBound event, simply add the following conditional:
if (myGridView.EditIndex == e.Row.RowIndex)
{
//do work
}
Thanks to Saurabh Tripathi,
The solution you provided worked for me.
In gridView_RowDataBound() event use.
if(gridView.EditIndex == e.Row.RowIndex && e.Row.RowType == DataControlRowType.DataRow)
{
// FindControl
// And populate it
}
If anyone is stuck with the same issue, then try this out.
Cheers.
I had the same issue, but this fix (Jason's, which is adding the conditional to the handler) didn't work for me; the Edit row never was databound, so that condition never evaluated to true. RowDataBound was simply never called with the same RowIndex as the GridView.EditIndex. My setup is a little different, though, in that instead of binding the dropdown programmatically I have it bound to an ObjectDataSource on the page. The dropdown still has to be bound separately per row, though, because its possible values depend on other information in the row. So the ObjectDataSource has a SessionParameter, and I make sure to set the appropriate session variable when needed for binding.
<asp:ObjectDataSource ID="objInfo" runat="server" SelectMethod="GetData" TypeName="MyTypeName">
<SelectParameters>
<asp:SessionParameter Name="MyID" SessionField="MID" Type="Int32" />
</SelectParameters>
And the dropdown in the relevant row:
<asp:TemplateField HeaderText="My Info" SortExpression="MyInfo">
<EditItemTemplate>
<asp:DropDownList ID="ddlEditMyInfo" runat="server" DataSourceID="objInfo" DataTextField="MyInfo" DataValueField="MyInfoID" SelectedValue='<%#Bind("ID") %>' />
</EditItemTemplate>
<ItemTemplate>
<span><%#Eval("MyInfo") %></span>
</ItemTemplate>
</asp:TemplateField>
What I ended up doing was not using a CommandField in the GridView to generate my edit, delete, update and cancel buttons; I did it on my own with a TemplateField, and by setting the CommandNames appropriately, I was able to trigger the built-in edit/delete/update/cancel actions on the GridView. For the Edit button, I made the CommandArgument the information I needed to bind the dropdown, instead of the row's PK like it would usually be. This luckily did not prevent the GridView from editing the appropriate row.
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="ibtnDelete" runat="server" ImageUrl="~/images/delete.gif" AlternateText="Delete" CommandArgument='<%#Eval("UniqueID") %>' CommandName="Delete" />
<asp:ImageButton ID="ibtnEdit" runat="server" ImageUrl="~/images/edit.gif" AlternateText="Edit" CommandArgument='<%#Eval("MyID") %>' CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="ibtnUpdate" runat="server" ImageUrl="~/images/update.gif" AlternateText="Update" CommandArgument='<%#Eval("UniqueID") %>' CommandName="Update" />
<asp:ImageButton ID="ibtnCancel" runat="server" ImageUrl="~/images/cancel.gif" AlternateText="Cancel" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
And in the RowCommand handler:
void grdOverrides_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
Session["MID"] = Int32.Parse(e.CommandArgument.ToString());
}
The RowCommand, of course, happens before the row goes into edit mode and thus before the dropdown databinds. So everything works. It's a little bit of a hack, but I'd spent enough time trying to figure out why the edit row wasn't being databound already.
protected void grdDevelopment_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (grdDevelopment.EditIndex == e.Row.RowIndex && e.Row.RowType==DataControlRowType.DataRow)
{
DropDownList drpBuildServers = (DropDownList)e.Row.Cells[0].FindControl("ddlBuildServers");
}
}
Try this one
This will help u
This code will be do what you want:
<asp:TemplateField HeaderText="garantia" SortExpression="garantia">
<EditItemTemplate>
<asp:DropDownList ID="ddgvOpp" runat="server" SelectedValue='<%# Bind("opponent.name") %>'>
<asp:ListItem Text="Si" Value="True"></asp:ListItem>
<asp:ListItem Text="No" Value="False"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("opponent.name") %>'></asp:Label>
</ItemTemplate>