I have a templatefield having a textbox and filteredtextboxextender inside the templatefield. I need to change the ValidChars property for filteredtextboxextender from "123" to "abc" in c# codebehind. The templatefield is there inside the GridView.
I used the following code in aspx page.
<asp:GridView ID="grdEducation" runat="server" AllowSorting="True" AutoGenerateColumns="False"
AllowPaging="false" CellPadding="4" GridLines="Vertical" OnRowDeleting="grdEducation_RowDeleting"
OnRowDataBound="grdEducation_RowDataBound" OnRowUpdating="grdEducation_RowUpdating" ShowFooter="false" ShowHeader="true">
<HeaderStyle CssClass="grid-header-style" />
<Columns>
<asp:TemplateField HeaderStyle-CssClass="grid-label-small" >`
<ItemTemplate>
<table>
<tr>
<td width='90%'>
<table>
<td width='60%'>
<asp:TextBox ID="textbox1" Width="100px" runat="server"
ToolTip="Provide text" MaxLength="11"></asp:TextBox>
<ajaxtoolkit:FilteredTextBoxExtender ID="filter" runat="server" TargetControlID="textbox1"
ValidChars="123" />
</td>
</table>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Is there any possibility is there for change filteredtextboxextender property like that like that?
Thank you..
Register the RowBoundData event like below.
protected void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
YourControlType Conrol = (YourControlType)e.Row.FindControl("ControlID");
//Set the property here
}
}
You can similarly change the Control Properties in Row_Command event also
Related
I am doing an interdisciplinary work in college which would be a simple POST system, using gridview and bootstrap/asp.net, so far it has worked out fine, but I am in the part of SHOWING the post on the home page, and the gridview is not very pretty , it does not look like a post, it looks like this:
Simply GridView:
And I wanted something like that:
The way I wanted it:
Do you have any way to do this?
If not, would I have to see the whole post click on the title and not the giant red button? Can not it be dark when I move the mouse? especially in pagination, like this: HORRIBLE
Code aspx :
<div class="container-fluid bg-page" id="conteudo">
<div class="row">
<div class="col-lg-12">
<asp:GridView ID="gdv_posts" runat="server" CssClass="table table-hover table-striped" GridLines="None" AutoGenerateColumns="false" OnRowCommand="gdv_posts_RowCommand" AllowPaging="True" OnPageIndexChanging="gdv_posts_PageIndexChanging">
<Columns>
<asp:BoundField DataField="titulo" HeaderText="Titulo" />
<asp:BoundField DataField="descrição" HeaderText="Descrição" />
<asp:BoundField DataField="data" HeaderText="Data de Criação" />
<asp:BoundField DataField="autor" HeaderText="Autor" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btn_visualizar" runat="server" Text="Visualizar" class="btn btn-danger" CommandName="Visualizar" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "id")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
</div>
Code cs:
protected void Page_Load(object sender, EventArgs e)
{
gdv_posts.UseAccessibleHeader = true;
if (!Page.IsPostBack)
{
preencherGrid();
}
}
protected void gdv_posts_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gdv_posts.PageIndex = e.NewPageIndex;
preencherGrid();
}
The <asp:GridView/> is basically a plain table int html. If you would like your posts to look like in the image supplied you would need a table of 1 column and X rows. To accomplish that you should use one <asp:TemplateField/> with an <ItemTemplate/> inside. And then design the content inside to your liking.
Example:
<asp:GridView ID="gdv_posts" runat="server" CssClass="table table-hover table-striped" GridLines="None" AutoGenerateColumns="false" OnRowCommand="gdv_posts_RowCommand" AllowPaging="True" OnPageIndexChanging="gdv_posts_PageIndexChanging">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "titulo")%>
</td>
</tr>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "descrição")%>
</td>
</tr>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "data")%>
</td>
</tr>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "autor")%>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I have a parent gridview with a child gridview
<!-- Parent -->
<asp:GridView ID="gvParent" runat="server" AutoGenerateColumns="false" Width="100%" CssClass="Grid"
DataKeyNames="SupplierReference" OnRowDataBound="gvParent_OnRowDataBound" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<img alt = "" style="cursor: pointer" src="images/plus.png" />
<asp:Panel ID="pnlOrders" runat="server" Style="display: none">
<!-- Child -->
<asp:GridView ID="gvChild" runat="server" AutoGenerateColumns="false" CssClass = "ChildGrid"
ShowFooter = "true" OnRowDataBound="gvChild_OnRowDataBound">
<Columns>
<asp:TemplateField HeaderText="Qty" ItemStyle-Width="100px">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Eval("Qty")%>' />
</ItemTemplate>
</asp:TemplateField >
</Columns>
</asp:GridView>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkSelectQtys" runat="server"
CommandArgument = '<%# Eval("SupplierReference")%>' CommandName="SelectQtys"
OnClientClick = "return confirm('Add these materials to this task?')"
Text = "Add" OnClick="getQty" ></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am trying to have a textbox that the user can alter the value and when they click add I want to be able to pull that text into the C# code and work with it.
I cant get it into my C# code.
protected void getQty(object sender, EventArgs e)
{
//After clicking "add"....
//Do something here to get text from each TextBox1 in the Child gridview
}
Someone please help before I lose what little hair I have left...
protected void getQty(object sender, EventArgs e)
{
//After clicking "add"....
string s;
for(i=0; i < gvChild.Rows.Count; i++)
{
s = ((TextBox)gvChild.Rows[i].FindControl("TextBox1")).Text;
}
//Do what you want to with this string
}
Actually I have problem with finding LinkButton Control in gridview .
I have 2 gridviews which one of them is inside another one so my problem is I cannot get the value of LinkButton of second gridview,
here is my code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BorderStyle="None" DataSourceID="SqlDataSource2" GridLines="None">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<table align="center" class="Table3">
<tr>
<td >
<asp:Label ID="lblID" runat="server" Visible="false" Text='<%# Eval("Food_ID") %>'></asp:Label>
<b> <%#Eval("Title")%></b>
</td>
</tr>
<tr>
<td align="center" class="ImgKidFood">
<asp:Image ID="Img" Width="680px" Height="145px" ImageUrl='<%#Eval("Pictures") %>' runat="server" />
</td>
</tr>
<tr>
<td style="direction:rtl; text-align:right;">
<asp:GridView ID="ShowFoodMenu2" runat="server" AutoGenerateColumns="False"
BorderStyle="None" GridLines="None" ShowHeader="False"
DataSourceID="SqlDataSource1" Width="100%"
>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table align="center" class="TableListMenu">
<tr>
<td class="Add">
<asp:LinkButton ID="LinkButton2" CommandArgument="<%# Container.DataItemIndex %>"
runat="server" CausesValidation="False"
CommandName="Select" Text="Select"
onclick="LinkButton2_Click" ></asp:LinkButton>
</td>
<td class="ToCenter">
<b><%#Eval("Title_Pr") %>
</b>
<asp:LinkButton ID="LinkButton12" runat="server" CommandArgument="<%# Container.DataItemIndex %>" CommandName="Link2" Text='<%# Eval("Menu_ID") %>'></asp:LinkButton>
</td>
<td class="PriceLeft">
<%#Eval("Price") %>
</td>
</tr>
</table>
<hr />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br /><br /><br />
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
As u can see the second gidview named id is ShowFoodMenu2 and it's inside first girdview which id Giridview1 .
and also I have two Linkbuttons inside the second gridview
one of them keep the value(which is LinkButton12) and another one(LinkButton2) is for when I clicked it add a record in Database .
But when I clicked on Linkbutton(LinkButton2) to show the value of Linkbutton12 , I will get ann error
Here is the error
http://hidelion.com/Images/error.png
and here is my.cs code
protected void LinkButton2_Click(object sender, EventArgs e)
{
GridView G = new GridView();
G.FindControl("ShowFoodMenu2");
System.Threading.Thread.Sleep(1000);
LinkButton m = (LinkButton)sender;
int i = Int32.Parse(m.CommandArgument);
LinkButton LblMososID = (LinkButton)G.Rows[i].FindControl("LinkButton2");
// LinkButton LblMososID2 = (LinkButton)G.Rows[i].FindControl("LinkButton12");
Label1.Text = LblMososID.Text;
}
So how can I solved this problem ??????
GridView G = new GridView(); just create a new instance of Gridview class, and it will not find your gridview automatically, You are messing it up, simply go like this, from linkbutton find the corresponding row, from that row fetch the control you wish to manipulate, do it like this:-
protected void LinkButton2_Click(object sender, EventArgs e)
{
LinkButton LinkButton2 = sender as LinkButton;
GridViewRow grdRow = (GridViewRow)LinkButton2.NamingContainer;
LinkButton LinkButton12 = (LinkButton)grdRow.FindControl("LinkButton12 ");
Label1.Text = LinkButton12.Text;
}
I am trying to make a GridView populate like the image below:
This is how my GridView currently looks.
Code:
<asp:GridView runat="server" ID="GridView1" CssClass="wiretable" AutoGenerateColumns="False"
BorderColor="#E8CC6B" BorderStyle="Solid" BorderWidth="1px"
Width="100%" ShowFooter="False" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Attendees" />
</Columns>
</asp:GridView>
Since you want a table structure, then I would recommend using a repeater instead of a grid to get better control of the output, like this:
Markup:
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table>
<tr>
<th colspan="2">Attendees</th>
<tr>
<tr>
</HeaderTemplate>
<ItemTemplate>
<%# (Container.ItemIndex != 0 && Container.ItemIndex % 2 == 0) ? #"</tr><tr>" : string.Empty %>
<%# string.Format("{0}{1}{2}", #"<td>", Container.DataItem, #"</td>") %>
</ItemTemplate>
<FooterTemplate>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
// Only bind repeater initially, not every post back
if (!IsPostBack)
{
Repeater1.DataSource = GetDataFromDatabase();
Repeater1.DataBind();
}
}
Note: You can apply whatever necessary CSS to the table header, rows and cells to make it look like you want.
Looking for:
<HeaderTemplate>
<table>
<tr>
<th colspan="2">Attendees</th>
<tr>
<tr>
</HeaderTemplate>
So HeaderTemplate tag's attributes.
So I have this problem with one of my pages, which basically lists out a bunch of comments for a specified student. The comments are supposed to be editable, but I'm having a problem with getting the contents of a row (so I can display the comment content in a TextBox and allow for edits).
The issue is that whenever I access the GridViewRow from the GridView like such:
this.CommentList.Rows[e.NewEditIndex]
It is returning a collection of cells, but whenever I try access the a cell like so: row.Cells[0].Text (where row is the selected GridViewRow object) it doesn't return any values.
Here is my .aspx code:
<asp:GridView ID="CommentList" runat="server" AutoGenerateColumns="False" CellPadding="5"
ShowHeader="false" Width="100%" DataKeyNames="CommentId" OnRowDeleting="CommentList_RowDeleting"
OnRowCancelingEdit="CommentList_RowCancelingEdit" OnRowEditing="CommentList_RowEditing">
<Columns>
<asp:TemplateField HeaderText="Student ID">
<ItemTemplate>
<b>Author:</b>
<%# ((Comment)Container.DataItem).Author.Name %>
<br />
<b>Date:</b>
<%# ((Comment)Container.DataItem).Created %>
<hr style="border-top: solid 1px black" />
<div style="text-align: center;">
<asp:Panel ID="OptionsPanel" runat="server">
<asp:LinkButton ID="DeleteLinkButton" CommandName="Delete" runat="server" OnClientClick="return confirm('Are you sure you want to delete this comment?');">Delete</asp:LinkButton>
|
<asp:LinkButton ID="EditLinkButton" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</asp:Panel>
<asp:Panel ID="EditOptionsPanel" runat="server" Visible="false">
<asp:LinkButton ID="CancelEditLinkButton" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</asp:Panel>
</div>
</ItemTemplate>
<ItemStyle CssClass="topLeftJustify" Width="200px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Panel ID="ViewCommentPanel" runat="server">
<%# ((Comment)Container.DataItem).Content%>
</asp:Panel>
<asp:Panel ID="EditCommentPanel" runat="server" Visible="false">
<asp:TextBox ID="Comment" runat="server" CssClass="textEntry" TextMode="MultiLine"
Width="100%" Height="100px"></asp:TextBox>
</asp:Panel>
</ItemTemplate>
<ItemStyle CssClass="topLeftJustify" />
</asp:TemplateField>
</Columns>
</asp:GridView>
Here is my .aspx.cs code (that relates to the code above):
protected void CommentList_RowEditing(object sender, GridViewEditEventArgs e)
{
try
{
GridViewRow row = this.CommentList.Rows[e.NewEditIndex];
((Panel)row.FindControl("OptionsPanel")).Visible = false;
((Panel)row.FindControl("EditOptionsPanel")).Visible = true;
((Panel)row.FindControl("ViewCommentPanel")).Visible = false;
((Panel)row.FindControl("EditCommentPanel")).Visible = true;
// problem is with this line. it doesn't show the contents of the "comment"
((TextBox)row.FindControl("Comment")).Text = row.Cells[0].Text;
}
catch (Exception ex)
{
this.Messages.ChangeMessage(ex.Message, MessageType.Error);
}
}
When you use a TemplateField you cannot use e.Row.Cells[index].Text to access a control text of the cell. I would simply add a Label to the panel.
But you can also try it this way:
Panel ViewCommentPanel = (Panel) e.Row.FindControl("ViewCommentPanel");
LiteralControl objPanelText = ViewCommentPanel.Controls[0] as LiteralControl;
TextBox Comment = (TextBox) row.FindControl("Comment");
Comment.Text = objPanelText.Text;