I'm trying to access server control that locate inside GridView, but I dont have idea how to do that..
Here is my try:
<asp:GridView ....
.....
<Columns>
<asp:TemplateField>
...
..
<EditItemTemplate>
<asp:TextBox ID="txtName"runat="server" Text='<%# Bind("Name") %>' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="edit" ItemStyle-CssClass="td"
HeaderStyle-CssClass="listtitle">
<ItemTemplate>
<asp:ImageButton ID="btnEdit" runat="server" CommandName="Edit"
AlternateText="edit" ImageUrl="~/css/images/edit-icon.png" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" runat="server" CommandName="Update" Text="Save"
OnClientClick="javascript:return isValid('<%= txtName.ClientID%>')" /> |
<asp:LinkButton ID="btnCancel" runat="server"
CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
...
..
</Columns>
.....
....
</asp:GridView>
The problem is in here
OnClientClick="javascript:return isValid('<%= txtName.ClientID%>')"
When it gets to the isValid function I see the string "<%= txtName.ClientID%>" and not the actual controller id as I should..
So the question is how can I send the id to the JS script?
ibrams answer would have been correct if it was the fields are on ItemTemplate. But this won't work when you are on EditItemTemplate one have to check the RowState also. The condition will be something like this
if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
{
//logic here
}
But a shorter solution will be checking this condition GridView1.EditIndex == e.Row.RowIndex
The snippet should be this.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && GridView1.EditIndex == e.Row.RowIndex)
{
TextBox txt = (TextBox)e.Row.FindControl("txtName");
LinkButton btn = (LinkButton)e.Row.FindControl("btnUpdate");
string script = String.Format("javascript:return isValid('{0}')", btn.ClientID);
btn.Attributes.Add("onclick", script);
}
}
On RowDataBound event you need to read the client id of your txtName and set the OnClientClick of your linkbutton.
void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txt = (TextBox)e.Row.FindControl("txtName");
Button btn = (LinkButton)e.Row.FindControl("btnUpdate");
btn.OnClientClick = String.Format("javascript:return isValid('{0}')", btn.ClientID);
}
}
As you want to validate value of the txtName textBox it's much more better utilize standart asp.net validation controls. If you have some complext validation rules then you can use CustomValidator and specify javascript function for processing validation on client side. Just set the same validatio ngroup value for the validator attached to the txtName control and for the btnUpdate button.
Related
i have a grid view in Asp.net. i want to generate button for specific rows.
Like i have a column named as "Status". If status is "Accept" a button should be generated in that particular row.
column
You can use a TemplateField in ASP.NET GridView and conditionally set the Button visibility, like shown below:
<asp:GridView runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="Accept"
Visible='<%# Eval("Status")=="Accept" %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Hope this will help.
first add template field button like this
<asp:TemplateField HeaderText="status" ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btn" runat="server" CausesValidation="false"
CommandName="Select" Text="button" />
</ItemTemplate>
</asp:TemplateField>
and codebehind
protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btn = (Button)e.Row.FindControl("btn");
if (e.Row.Cells[1].Text == "Accept")//replace 1 by your column(status) index in gridview
{
btn.Visible = true;
}
else
{
btn.Visible = false;
}
}
}
and to add code to this button so add it on
protected void grid_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
//code
}
i have a gridview ,whenever user will click on row of gridview view ,
record will be populated in the textbox ,but i am getting error as
index was out of range. must be non-negative and less than the size of
the collection
also i check gridview.row.count is showing zero.
please help below is my code
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" ShowHeader="false" AllowPaging="false" AllowSorting="false" ToolTip="Click Here To Edit"
Style="table-layout: fixed;" OnRowDataBound="GridView1_RowDataBound"
CssClass="mGrid" PagerStyle-CssClass="pgr" DataKeyNames="AcheadID"
AlternatingRowStyle-CssClass="alt" Width="100%" Height="100%" >
<AlternatingRowStyle CssClass="alt" />
<Columns>
<asp:TemplateField ItemStyle-Width="40px">
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Account Head ID" DataField="AcheadID" HeaderStyle-HorizontalAlign="Left" Visible="false" />
<asp:TemplateField HeaderText="Account Head" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="400px">
<ItemTemplate>
<asp:Label ID="lblac_head" runat="server" Text='<%# Bind("ac_head") %>'></asp:Label>
<asp:HiddenField ID="hndacheadID" runat="server" Value='<%# Bind("AcheadID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="btnTest" runat="server" OnClick="GridView1_OnClick" style="display:none;" />
my codebehind as
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetAccountHead();
}
}
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HiddenField hndac_headid = (HiddenField)e.Row.FindControl("hndac_headid");
if (hndac_headid.Value != "0")
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#d3d3d3'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
e.Row.Attributes.Add("style", "cursor:pointer;");
//e.Row.Attributes.Add("onclick", "location='patron_detail.aspx?id=" + e.Row.Cells[0].Text + "'");
e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(btnTest, e.Row.RowIndex.ToString());
}
}
}
protected void GridView1_OnClick(object sender, EventArgs e)
{
Button buttonSender = sender as Button;
int index;
GridViewRow row = null;
if (int.TryParse(Request.Params.Get("__EVENTARGUMENT"), out index))
{
row = GridView1.Rows[index];
Label ac_head = (Label)GridView1.Rows[index].FindControl("lblac_head");
}
Index out of range issue happens because Request.Params.Get("__EVENTARGUMENT") doesn't works for Button ( and addiionally ImageButton ) controls.
The Button controls and ImageButton controls does not call the __doPostBack function. Because of this, the _EVENTARGUMENT ( and _EVENTTARGET as well ) will always be empty.
However, other controls such as CheckBoxes, DropDownLists, LinkButtons uses javascript function __doPostBack to trigger a postback.
Try using a LinkButton
As a second check( can be ignored ), just make sure Data is binded to GridView properly like check the IsPostback conditions etc...
Check your GridView, it has an Id GridView2 but you always referencer GridView1.
I Have a LinkButton In a TemplateField In a GridView. The LinkButton is bound to database which shows the time save in database.All I want is to fetch each linkbutton time value and compare it to current time and if linkbutton time is less than current time ,make it disable.
Any pointers will be helpful.
Thanks in advance.
<asp:GridView ID="grdview" AutoGenerateColumns="false" runat="server" OnRowCommand="grdview_RowCommand">
<Columns>
<asp:BoundField DataField="AudiName" DataFormatString="Audi {0}" HeaderText="Audi Name" />
<asp:TemplateField HeaderText="StartTime">
<ItemTemplate>
<asp:LinkButton ID="lnkmovietime" runat="server"
Text='<%# Eval("StartTime") %>'
CommandName="time"
CommandArgument='<%#Eval("AudiID") %>'>
</asp:LinkButton>
<asp:HiddenField runat="server" ID="hf1" Value='<%#Eval("StartTime")%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You can use the OnRowDataBound event to hook into the data binding and disable the button.
Form
<asp:GridView ID="MyGridView"
OnRowDataBound="MyGridView_RowDataBound"
runat="server">
....
</asp:GridView>
CodeBehind
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// Find the button
var linkButton = (LinkButton) e.Row.FindControl("MyLinkButton");
// Toggle enabled/disabled based on time
// Using the button text is probably a bad idea, but is used here for demonstration purposes
linkButton.Enabled = (Convert.ToDateTime(linkButton.Text) > DateTime.Now);
}
}
The code above has not been tested, but should give you an idea of how you can approach this.
You can set the Enabled property of LinkButton comparing with the current time:
<asp:LinkButton ID="lnkmovietime" runat="server"
Text='<%# Eval("StartTime") %>'
Enabled = '<%# ((DateTime)Eval("StartTime")< DateTime.Now )? false : true %>'
CommandName="time" CommandArgument='<%#Eval("AudiID") %>'>
</asp:LinkButton>
How do I use a GridView TemplateField containing a LinkButton that is to display the modal on click? I have rows of data that I want to update the details of when clicking the 'Edit' LinkButton in that row. There is a bunch of data I need to load via codebehind just before the Modal displays.
I was trying the following, but I can't do Modal1.Show() in the event handler because it's in a TemplateField:
<ItemTemplate>
<asp:Button runat="server" ID="HiddenForModal" style="display: none" />
<ajaxToolkit:ModalPopupExtender ID="Modal1" runat="server" TargetControlID="HiddenForModal" PopupControlID="pnlModal" />
<asp:LinkButton ID="btnEdit" runat="server" Text="Edit" onclick="btnEdit_Click" />
<asp:LinkButton ID="btnDelete" runat="server" Text="Delete"></asp:LinkButton>
</ItemTemplate>
Thanks,
Mark
The key is knowing which row in the GridView was the LinkButton that was clicked. You can do this several ways but the way I implemented it is to capture it in the RowCommand event. Then you can access the ModalPopupExtender in the clicked row via FindControl(..).
Page:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" style="Display:none;" Text="Button" />
<cc1:ModalPopupExtender ID="ModalPopupExtender1" PopupControlID="Popup1" TargetControlID="Button1" BackgroundCssClass="modalBackground" runat="server" />
<asp:LinkButton ID="LinkButton1" CommandName="Popup" runat="server">Popup</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Codebehind:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton LinkButton1 = (LinkButton)e.Row.FindControl("LinkButton1");
LinkButton1.CommandArgument = e.Row.RowIndex.ToString();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Popup" && e.CommandArgument != null)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
ModalPopupExtender modalPopupExtender1 = (ModalPopupExtender)GridView1.Rows[rowIndex].FindControl("ModalPopupExtender1");
modalPopupExtender1.Show();
//Perform any specific processing.
Label1.Text = string.Format("Row # {0}", rowIndex);
}
}
Additionally, because you are opening the modal on a postback anyways you don't actually need the ModalPopupExtender (or the hidden button) in the ItemTemplate. You can move that out and put it on the page (by your popup div) and can simply call the Show() method.
Page:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" CommandName="Popup" runat="server">Popup</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Codebehind:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Popup" && e.CommandArgument != null)
{
int rowIndex = Convert.ToInt32(e.CommandArgument);
ModalPopupExtender1.Show();
//Perform any specific processing
Label1.Text = string.Format("<Br>Row # {0}", rowIndex);
}
}
Thanks, good luck!
I'm defining a GridView as follows:
<asp:GridView ID="MediaViewsGrid" DataSourceID="MediaViewsDataSource"
runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:CommandField ShowEditButton="True" ShowDeleteButton="true" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblLanguage" runat="server" Text='<%# Eval("Language.Name") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlLanguages"
DataSourceID="LanguageDataSource"
DataTextField="Name"
DataValueField="Code"
OnDataBound="ddlLanguages_OnDataBound"
runat="server" />
</EditItemTemplate>
</asp:TemplateField>
I'm trying to set a default value for my ddlLanguages drop down list when user clicks on "edit". Is the correct solution to define an OnDataBound function and set the selected index there?
If so, how do i do it? Or is there a better solution?
Your subject says datagrid, but your code says gridview. I'll assume gv.
Here's a sample:
protected void gvOrderSummary_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow &&
(e.Row.RowState == DataControlRowState.Edit ||
(e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))))
{
((DropDownList)e.Row.Cells[4].Controls[1]).SelectedValue = DataBinder.Eval(e.Row.DataItem, "orderStatusId").ToString();
}
}