gridview edit requires to click twice - asp.net

Why is that I need to click the edit link twice, in a gridview control, before my row enters into edit mode?
<asp:ObjectDataSource ID="ods" runat="server" TypeName="Employee"
SelectMethod="GetAll" ></asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" CssClass="styled"
OnRowCommand="gv_RowCommand" DataSourceID="ods"
OnSorting="gv_Sorting" >
<Columns>
...........
</Columns>
<ItemTemplate>
<ItemTemplate>
<div class='actions'>
<asp:Button ID="btnEdit" runat="server" Text=" Edit " ToolTip="Edit Row" CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"Id") %>' CausesValidation="False" />
<span style="padding-left:10px"></span>
</div>
</ItemTemplate>
</asp:GridView>
protected override void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.ods.SelectParameters[0].DefaultValue = "";
}
}
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == CRUID.Edit.ToString())
{
this.gv.ShowFooter = false;
}
}

You need to avoid rebinding your gridview on each postback.
If not ispostback then
GridView1.DataSource = dt
GridView1.DataBind()
end if
Otherwise you just overwrite Gridview changes.
Great explanation at this link...
http://www.pcreview.co.uk/forums/gridview-two-clicks-needed-enter-place-editing-t3328887.html

Try handling the RowEditing event to set the EditItem Index:
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex
}

There are some mistakes in your code as i examined. Correct your code as shown below:
<asp:ObjectDataSource ID="ods" runat="server" TypeName="Employee"
SelectMethod="GetAll" ></asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" CssClass="styled"
OnRowCommand="gv_RowCommand" DataSourceID="ods"
OnSorting="gv_Sorting" >
<Columns>
...........
<asp:TemplateField>
<ItemTemplate>
<div class='actions'>
<asp:Button ID="btnEdit" runat="server" Text=" Edit " ToolTip="Edit Row" CommandName="Edit" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"Id") %>' CausesValidation="False" />
<span style="padding-left:10px"></span>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected override void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.ods.SelectParameters[0].DefaultValue = "";
}
}
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
this.gv.ShowFooter = false;
}
}
If on using this code the problem does not solve then there may be some problem in your cssclass which you used with your GridView as I have Checked your code on my machine using ObjectDataSource and it works well using edited code.
Also I want to know that what is CRUID in CRUID.Edit.ToString()
and why you used the following line in Page_Load event
this.ods.SelectParameters[0].DefaultValue = "";
as there are no parameter associated with your SelectMethod="GetAll" method used in ObjectDataSource.
May this answer help you.

I guess there is some conflict with the updatepanels on your page..
Try removing all your Update Panels and try again.. It will work for sure.. Mine worked a few seconds ago.. so thought It would be good to share..

Related

how to hide a linkbutton onload in asp.net

I have a linkbutton inside a listview.I want to know how can i hide this linkbutton on certain conditions.
My Codes
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" OnClientClick="return deleteConfirm();">
<img src="../Admin/Images/deletebtn.png"alt="deletebtn" class="deleteimgbtn" id="dltbtn"/>
</asp:LinkButton>
protected void Page_Load(object sender, EventArgs e)
{
ListView1.FindControl("LinkButton1").Visible = false;//Iam tried by using this code,but doesn't work
}
When defining your ListView template, it is just that ... a template, so the buttons don't yet exist. You will therefore need to use the ListView.ItemDataBound event to find and disable your button as each item is being bound.
<asp:ListView ID="MyListView" runat="server" OnItemDataBound="MyListView_ItemDataBound">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" OnClientClick="return deleteConfirm();">
<img src="../Admin/Images/deletebtn.png"alt="deletebtn" class="deleteimgbtn" id="dltbtn"/>
</asp:LinkButton>
</ItemTemplate>
</asp:ListView>
protected void MyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
var LinkButton1 = (LinkButton)e.Item.FindControl("LinkButton1");
if (true) // insert your condition here
{
LinkButton1.Visible = false;
}
}
}

How to get Reference to the label in repeater item in code behind

<asp:repeater id="rpt" run="server">
<ItemTemplate>
<asp:LinkButton id="Delete" runat="server" OnCommand="Delete_Command"></asp:linkButton>
<asp:label id="lblMessage" run="server">
</ItemTemplate>
</asp:repeater>
Code Behind:
protected void Delete_Command(object sender, CommandEventArgument e)
{
}
how i get the reference to the "lblMessage" in Delete_Command.
Try this:
protected void Delete_Command(object sender, CommandEventArgs e)
{
LinkButton button = (LinkButton)sender;
Label label = (Label)button.NamingContainer.FindControl("lblMessage");
// do something with the label
}
If you:
Have bound the repeater
Have ViewState enabled
Do not re-bind the repeater earlier in the post back
this should work. If not, please verify that the id of the label is indeed exactly the same as in the ...FindControl("lblMessage");. Also make sure that runat="server" is set on all the controls involved.
Edit: One more thing to check: Search the markup file (the .aspx file) and check if there are any other controls that also use the same event in the code behind. If another control is using the same event handler and that control is not in the repeater, the label will not be found.
means are you want find a lable in Delete_Command event?
in aspx
<asp:Repeater ID="rpt" runat="server">
<ItemTemplate>
<asp:LinkButton ID="Delete" runat="server" OnCommand="Delete_Command"></asp:LinkButton>
<asp:Label ID="lblMessage" run="server">
</ItemTemplate>
</asp:Repeater>
in aspx.cs
protected void Delete_Command(object sender, CommandEventArgs e)
{
foreach (RepeaterItem item in rpt.Items)
{
Label lblMessage = item.FindControl("lblMessage") as Label;
if (lblMessage != null)
{
lblMessage.Text = "";
}
}
}
If You want to make it in your way use following code in
protected void Repeater1_ItemCommand(object source, CommandEventArgs e)
{
(((LinkButton)source).NamingContainer).FindControl("lblName")
}
Another approach.. But something that you can buy
aspx
<asp:Repeater ID="Repeater1" runat="server"
onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%=Eval("Name") %>' ></asp:Label>
<asp:LinkButton runat="server" CommandName="Delete_Command" Text="sd"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
.cs
protected void Delete_Command(object sender, CommandEventArgument e)
{
if(e.CommandName != null)// Conditional Check
{
Label label = e.Item.FindControl("lblMessage");
// do something with the label
}
}

ASP.NET - Finding Label Control in a GridView

I'm having a problem trying to find a label control that is inside a GridView.
Please see my codes below:
<asp:GridView ID="MyGridView" runat="server">
<Columns>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtDate" MaxLength="10" Width="70" />
<asp:ImageButton ID="imgScoreDate" runat="server" ImageUrl="~/images/calendar.gif" />
<ajaxtoolkit:CalendarExtender ID="txtDate_CalendarExtender" runat="server" Enabled="True" Format="MM/dd/yyyy" TargetControlID="txtDate" PopupButtonID="imgDate" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And here is my .cs file:
protected void LoadGridView()
{
//Do something else
foreach (GridViewRow row in MyGridView.Rows)
{
//Tried A
System.Web.UI.WebControls.Label lblName = row.FindControl("lblName") as System.Web.UI.WebControls.Label;
lblName.Text = "Name";
//Tried B
((System.Web.UI.WebControls.Label)row.FindControl("lblName")).Text = "Name";
}
}
I debug this code and it seems to work fine because my breakpoint is being hit each time the debugger runs. It even loops through my foreach block the same count as to how many rows my GridView has.
But I don't understand why my lblName control doesn't get the "Name" text as a value? Am I missing anything here? I tried both //Tried A and //Tried B methods but they both doesn't update my label's text.
Any help would be appreciated!
Thanks! Cheers!
You want to call LoadGridView inside PreRender. Basically, you want to call it after GridView is bound with data.
protected void Page_PreRender(object sender, EventArgs e)
{
LoadGridView();
}
Look at PreRender event of ASP.NET Page Life Cycle.
On your gridview add:
<asp:GridView OnRowDataBound="MyGridView_RowDataBound" ... />
Then define MyGridView_RowDataBound:
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
Label l = (Label) e.Row.FindControl("lblName");
}
What I think is happening is the control is not recreated server side in its current spot.
try this
on .aspx page
<asp:GridView ID="MyGridView" runat="server"
onrowdatabound="MyGridView_RowDataBound" .../>
code behind ::
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadGridView();
}
}
void LoadGridView()
{
DataTable dt = new DataTable();
// dt= call ur database method to get data
MyGridView.DataSource = dt;
MyGridView.DataBind();
}
protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl_Name = (Label)e.Row.FindControl("lblName");
lbl_Name.Text = "Name";
}
}
cheers!

Getting row from hyperlink in Gridview

I have a Hyperlink field in a asp.net gridview that runs code behind in an aspx file, like
<asp:GridView ID="gvCoursesList" runat="server">
<Columns>
<asp:BoundField DataField="Course" HeaderText="Course">
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lblSignup" runat="server" Text="Sign Up" OnClick= "lblSignup_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In my code behind file I have the function:
protected void lblSignup_Click(object sender, EventArgs e)
{
}
How do I retrieve the value of the first column from this function.
I would like to do something like
string course = gvCoursesList.DataKeys[e.RowIndex].Value.ToString();
but it's not working. Any other ideas?
#jishnusaha should be the approach you should take. Here is another alternative: http://forums.asp.net/p/1137287/1821091.aspx
protected void lblSignup_Click(object sender, EventArgs e)
{
LinkButton btn = sender as LinkButton;
GridViewRow row = btn.NamingContainer as GridViewRow;
string course = gvCoursesList.DataKeys[row.RowIndex].Value.ToString();
}
<asp:GridView ID="gvCoursesList" runat="server" OnRowCommand="gvCoursesList_RowCommand">
<Columns>
<asp:BoundField DataField="Course" HeaderText="Course">
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lblSignup" runat="server" Text="Sign Up" CommandName="SignUp" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
protected void gvCoursesList_RowCommand(object sender, GridViewRowCommandEventArgs e)
{
if(e.CommandName=="SignUp")
{
string course = gvCoursesList.DataKeys[e.RowIndex].Value.ToString();
}
}
Use the .cells.text to get the value out:
protected void lblSignup_Click(object sender, EventArgs e)
{
GridViewRow row = gvCoursesList.rows[e.rowindex]
string course = row.cells[cellIndex].text;
}
With thanks to codingbiz. His solution just needed a little tweak as per below.
LinkButton btn = sender as LinkButton;
GridViewRow row = btn.NamingContainer as GridViewRow;
string course = row.Cells[0].Text;

Trying to convert functional asp:CommandField to asp:LinkButton via asp:TemplateField

I want to take advantage of the breadth of attributes available to a LinkButton compared to a CommandField. When calling the code behind method from on click I get this error message:
CS0123: No overload for 'ViewHandler' matches delegate
'System.EventHandler'.
GridView definition:
<asp:GridView ID="wc" EmptyDataText="Empty WC table..." runat="server"
AutoGenerateColumns="False" GridLines="Horizontal" DataKeyNames="WC_ID,PW_ID,R_Qty"
CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
AllowSorting="true" OnSorting="Sorting" AllowPaging="true" PageSize="15" ViewStateMode="Enabled"
OnSelectedIndexChanging="ViewHandler" OnPageIndexChanging="HandlePageIndexChanging" OnRowDataBound="gvRowDataBound"
OnRowEditing="gvRowEditing" OnRowCancelingEdit="gvRowCancelingEdit" OnRowUpdating="gvRowUpdating">
Orig button...
<asp:CommandField ControlStyle-Font-Size="Smaller" ControlStyle-ForeColor="#717171" HeaderText="..." ShowSelectButton="True" ItemStyle-HorizontalAlign="Center" SelectText="..." />
Convert to....
<asp:TemplateField HeaderText="..." ShowHeader="True" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton style="font-size: 0.9em; color: #717171" ID="Details" runat="server" CausesValidation="False" OnClick="ViewHandler" CommandName="ViewHandler" Text="..." ToolTip="Click here to see purchase history and notes"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Code behind:
protected void ViewHandler(object sender, GridViewSelectEventArgs e)
{
string WC_ID =wc.Rows[e.NewSelectedIndex].Cells[0].Text;
string PW_ID = wc.Rows[e.NewSelectedIndex].Cells[1].Text;
string VIN = wc.Rows[e.NewSelectedIndex].Cells[2].Text;
string PROD = wc.Rows[e.NewSelectedIndex].Cells[3].Text;
string W = wc.Rows[e.NewSelectedIndex].Cells[4].Text;
string SIZE = wc.Rows[e.NewSelectedIndex].Cells[5].Text;
e.Cancel = true;
Session.Add(WebConstants.WC_ID, WC_ID);
Session.Add(WebConstants.PW_ID, PW_ID);
Session.Add(WebConstants.VIN, VIN);
Session.Add(WebConstants.PROD, PROD);
Session.Add(WebConstants.W, W);
Session.Add(WebConstants.SIZE, SIZE);
DetailsNavigateMethod.DynamicInvoke();
HandlePageNavigation(WebConstants.WC_DETAILS, "WC View for WC_PurchaseID-> " + Session[WebConstants.WC_ID].ToString());
}
What do I need to do to make the signature match with the ViewHandler method? I did notice if I switch from GridViewSelectEventArgs to EventArgs the overload error goes away but then e.NewSelectedIndex does not exists...
Thanks!
Your problem is that the OnClick event handler for a LinkButton must have this signature:
protected void LinkButton_Click(Object sender, EventArgs e)
But you are defining it as
protected void ViewHandler(object sender, GridViewSelectEventArgs e)
However, you won't have access to e.NewSelectedIndex, obviously. What you can do is change your markup to (notice the CommandArgument part):
<asp:TemplateField HeaderText="..." ShowHeader="True" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton style="font-size: 0.9em; color: #717171" ID="Details" runat="server" CausesValidation="False" OnClick="ViewHandler" CommandName="ViewHandler" CommandArgument='<%=string.Format("{0},{1},{2},{3}",Eval("Prop1"),Eval("Prop2"),Eval("Prop3"),Eval("Prop4")) %>' Text="..." ToolTip="Click here to see purchase history and notes"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
And your code to:
protected void LinkButton_Click(Object sender, EventArgs e)
{
string [] values = ((LinkButton)(sender)).CommandArgument.Split(new char[]{','});
//where values[0] will have WC_ID, values[1]=PW_ID, etc,
Session.Add(WebConstants.WC_ID, values[0]);
Session.Add(WebConstants.PW_ID, values[1]);//etc...
//Session.Add(WebConstants.VIN, VIN);
//Session.Add(WebConstants.PROD, PROD);
//Session.Add(WebConstants.W, W);
//Session.Add(WebConstants.SIZE, SIZE);
DetailsNavigateMethod.DynamicInvoke();
HandlePageNavigation(WebConstants.WC_DETAILS, "WC View for WC_PurchaseID-> " + Session[WebConstants.WC_ID].ToString());
}
Note:
Prop1, Prop2, etc. have to be changed to actual property names from your data source.
Above code hasn't been tested but that's the idea.

Resources