I have table with column MasName of type nvarchar(100). I am using SQL Server 2012 and Entity Framework 6 to work with database. I have DetailsView where I always bind one relevant record:
<asp:DetailsView ID="masDetailView" runat="server" AutoGenerateRows="False"
SelectMethod="GetMas" UpdateMethod="UpdateMas" ItemType="NSMAS.DAL.Mas"
AutoGenerateEditButton="True">
<Fields>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="nameLbl" Text='<%#: Item.MasName %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="nameEdit" runat="server" Text='<%#: Item.MasName %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<%--other columns--%>
Text binded to Label nameLbl show correctly: Hříběcí hory, but when I click auto generated edit button and text is binded to TextBox nameEdit (for editing entry), text is: Hříběcí hory.
Where could be problem? Can I fix this for all records or do I have to set encoding to every TextBox (that I don't know how since I don't bind text in codebehind)
EDIT:
So I have rewrited Text='<%#: Item.MasName %>' to Text='<%# Item.MasName %>' and now it works. I was told that it's better to use : after <%# when I access data like this, but I can't find any article about what is this exactly doing.
Related
I am having trouble retrieving the new value that is entered in a textbox template field in my GridView.
Here is my markup:
<asp:TemplateField HeaderText="username" SortExpression="username">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("username") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtUserName" runat="server" Text='<%# Bind("username") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
And here is how I'm trying to retrieve the new value, inside the RowCommand event handler of my GridView:
string userName = ((TextBox)grdUserList.Rows[rowIndex].FindControl("txtUserName")).Text;
I get the old value instead of the newly typed value when I execute this code.
Does anyone know what I am missing? Thanks in advance.
I just found out the solution for my problem. I searched and found out that the GridView is being refreshed before the retrieving process begins because I was rebinding the GridView on the Page_Load method. I fixed the problem by not rebinding the gridview when it is a post back (or at least not before I have made the changes) using the IsPostback method. Thanks for everyone's reply :)
You are retrieving new value in wrong GridView event. You have to add OnRowUpdating="grdUserList_RowUpdating" event in your GridView control and then retrieve new TextBox value.
OnRowUpdating event in code-behind:
protected void grdUserList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string userName = ((TextBox)grdUserList.Rows[e.RowIndex].FindControl("txtUserName")).Text;
// Write your update query and logic over here.
}
You can take a reference from here for additional knowledge.
Please let me know if you have any questions.
use this code in gridview
<Columns>
<asp:TemplateField HeaderText="SrNo">
<EditItemTemplate>
<asp:TextBox ID="txtsrno" runat="server" Text='<%#Eval("SrNo") %>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblsrno" runat="server" Text='<%#Eval("SrNo") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
I'm wondering if it is possible to format a Grid View like the pattern below
Usual Grid View:
Name Address Age Gender <--- Fields Name
Example Example Example Example <--- Values
What I want to look like
"Fields" "Values"
Name Example
Address Example
Age Example
Gender Example
Any thought will be highly appreciated
you should look into using a repeater.
I think that GridView was simply not meant to be used that way. Usually, you will display several items in it and if those items are too many you will end up with the horizontal scrolling mistake (also, some nice arguments here).
If you are showing only one record, you should use a DetailsView control, which:
Displays the values of a single record from a data source in a table,
where each data row represents a field of the record. The DetailsView
control allows you to edit, delete, and insert records.
I got an Answer using this Codes:
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="">
<ItemTemplate>
Name: <asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>'></asp:Label><br />
Address: <asp:Label ID="Label2" runat="server" Text='<%# Eval("Address") %>'></asp:Label><br />
Postcode: <asp:Label ID="Label3" runat="server" Text='<%# Eval("Postcode") %>'></asp:Label><br />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I have a gridview which has a few rows (each with a unique rowId), and each line has a FileUpload control, now everything works okay with FileUpload.
(my uploaded file database image can be seen below)
I have the download button, which also works okay, however I want to make this button invisible if no file exists for the corresponding row.
Nothing proper comes to my mind.
My button and FileUpload control:
<asp:TemplateField HeaderText="BatchList">
<EditItemTemplate>
<asp:ImageButton ID="ibt_Download" runat="server" src="Images/Download.png" CommandName="Download" CommandArgument='<%# Container.DataItemIndex %>' ></asp:ImageButton>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UploadBatchList">
<HeaderTemplate>
<asp:Label ID="lbl_Header" ToolTip="Upload Batch List" runat="server" Text="UBL"></asp:Label>
</HeaderTemplate>
<EditItemTemplate>
<asp:FileUpload ID="fu_UploadBatchList" runat="server" />
<asp:Button ID="btn_Upload" runat="server" Text="Upload" OnClick="btn_Upload_Click" />
</EditItemTemplate>
</asp:TemplateField>
This is how it looks on my gridView
When gridview is first created the green dots must not be visible if a file has been uploaded before.
My file database:
You can check some property of the data item (DocName in your case) if it contains a value (it might not work when copy-pasted, I'm a little bit improvising):
<asp:Button ID="btn_Upload" runat="server"
Text="Upload"
Visible='<% DataBinder.Eval(Container.DataItem, "DocName") == null %>
OnClick="btn_Upload_Click" />
Or you can create a function that will evaluate the visibility. See Mastering ASP.NET DataBinding for more.
i think this maybe an easy one...
I have a Gridview, bound to a dataSource with this query:
select userid, name, phone from users
I need to change the content of the Name cell to a link, like so:
User's Name
so OnRowDataBound i'm doing this:
e.Row.Cells[1].Text = "" + e.Row.Cells[1].Text + "";
It all works fine this way. My problem is that i don't want to display the UserId column in the Gridview, but when i attribute it (asp attribute or server-side) Visible="false" to the BoundField UserId, i can't capture it's value to build the Name Cell.
Thanx for your help
Make your gridView to use template items instead of simple grivdiew column.
<asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FirstName")%>'>
</asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("FirstName") %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
You can find a nice intro Here
Agree with Jani, this should be done with a template field. If you really need to do it the other way, see Can data be kept in a dynamically bound GridView's invisible fields?.
you can set the visibility on the onrowdatabound event
Ok, this should be really easy, but I just don't have enough experience.
I need to throw a GridView on a WebForm and populate with a List, where Template is my class that has ID, Name, CreatedOn, etc... properties.
The GridView needs to display each Template Name as a link. The link should point to TemplateEdit.aspx page, with the following URL: TemplateEdit.aspx?ID={ID of Template}.
I also need a Delete link (preferably an image link), that should popup a Yes/No delete confirmation dialog.
I've actually done this before in 2005 or so, but I simply can't remember anymore.
Here's how you do it (borrowed the code from here to save some typing)
<asp:TemplateField HeaderText="Statement" SortExpression="Statement">
<ItemTemplate>
<asp:HyperLink ID="Link1" runat="server" NavigateUrl='<%# Bind("ID", "~/TemplateEdit.aspx?ID={0}") %>' Text="The Best Link"></asp:HyperLink >
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="DeleteButton" Runat="server" ImageUrl="~/images/delete.gif" OnClientClick="return confirm('Are you sure you want to delete this?');" ToolTip="Delete" CommandName="Delete" />
</ItemTemplate>
</asp:TemplateField>
didn't actually test it, but looks like it should work.