ImageButton In UpdatePanel Doesn't Firing [Asp.net] - c#

i'm have a problem with button clicked in asp.net update panel. Here My Code :
My Aspx Code :
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
<ContentTemplate>
<div class="modal fade modal-lg" id="mdlVendor" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="mdlVendorLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="mdlVendorLabel">Vendor</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-3">
<asp:Label runat="server" ></asp:Label>
<asp:TextBox runat="server" ID="txtVendorSearch" CssClass="form-control me-2" OnClientClick="javascript:return false;" data-target="#mdlVendor" OnTextChanged="txtVendorSearch_TextChanged" AutoPostBack="true"></asp:TextBox>
<br />
</div>
<div class="col-md-3">
</div>
</div>
<asp:GridView ID="gvVendor" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" Width="100%" OnRowCommand="gvVendor_RowCommand">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<EmptyDataTemplate>
<asp:Label runat="server" ID="lblEmptyData" Text="No Data Found"></asp:Label>
</EmptyDataTemplate>
<Columns>
<asp:TemplateField HeaderText="Vendor Id">
<ItemTemplate>
<asp:Label runat="server" ID="VendorID" Text='<%# Eval("VendorID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Vendor Name">
<ItemTemplate>
<asp:Label runat="server" ID="VendorName" Text='<%# Eval("VendorName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ImageUrl="~/img/add.png" runat="server" Width="20px" Height="20px" CommandName="SelectDataVendor" ToolTip="Select Data Vendor"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
My Aspx.cs Code :
protected void gvVendor_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("SelectDataVendor"))
{
GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
int index = row.RowIndex;
GridViewRow clickedRow = gvVendor.Rows[index];
Label VendorName = (Label)clickedRow.Cells[2].FindControl("VendorName");
txtVendor.Text = VendorName.Text;
}
}
protected void txtVendorSearch_TextChanged(object sender, EventArgs e)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "openModal();", true);
DataTable dt = new DataTable();
string StrSeacrh = txtVendorSearch.Text;
dt = trxExpenditureCls.GetVendor(StrSeacrh);
if (dt.Rows.Count > 0)
{
gvVendor.DataSource = dt;
gvVendor.DataBind();
}
else
{
dt.Rows.Add(dt.NewRow());
gvVendor.DataSource = dt;
gvVendor.DataBind();
gvVendor.Rows[0].Cells.Clear();
gvVendor.Rows[0].Cells.Add(new TableCell());
gvVendor.Rows[0].Cells[0].ColumnSpan = dt.Columns.Count;
gvVendor.Rows[0].Cells[0].Text = "No Data Found";
gvVendor.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
}
}
When i'm click imageButton :
<asp:ImageButton ImageUrl="~/img/add.png" runat="server" Width="20px" Height="20px" CommandName="SelectDataVendor" ToolTip="Select Data Vendor"/>
system doesn't debug into gvVendor_RowCommand. When i make breakpoint in Page_Load, debugger goes into method gvVendor_RowCommand too. But i get a problem, txtVendor doesn't filled after debugger finish.
I'm already try :
<asp:UpdatePanel runat="server" ID="updPnl2" UpdateMode="Conditional">
<ContentTemplate>
<asp:ImageButton ImageUrl="~/img/add.png" runat="server" Width="20px" Height="20px" CommandName="SelectDataVendor" ToolTip="Select Data Vendor" ID="btnAdd"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger controlid="btnAdd" eventname="Click" />
</Triggers>
</asp:UpdatePanel>
And both
<asp:AsyncPostBackTrigger controlid="btnAdd" eventname="Click" />
Or
<asp:PostBackTrigger controlid="btnAdd" />
None of All fix my problem, even i have tried :
<asp:UpdatePanel runat="server" ID="updPnl2" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button runat="server" ID="btnAddNewData" OnClick="bntAddNewData_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger controlid="btnAddNewData" eventname="Click" />
</Triggers>
</asp:UpdatePanel>
Still doesn't fix my problem, is anyone can help me? i'm stuck here. Thanks you before

Hum, I don't think the introduction of a update panel should matter.
On the other hand? I long ago dumped the use of the row command.
Most don't realize that you can drop in a standard asp.net button, or image button.
Just wire up a plane jane click event for the code behind. The beauty of this approach is you don't have to clutter up bunch of events into the row command.
(VERY little reason to bother or use that).
So, say we drop in a button into the GV.
Now, we can't double click on the button in the designer to auto wire up a event, but you can do this in the markup. Just type in onclick= -- and intel-sense will pop this up:
So, choose create new event.
And now you have this code for the image button click event:
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ImageButton btn = (ImageButton)sender;
GridViewRow gRow = (GridViewRow)btn.NamingContainer;
// get pkID
int PKID = (int)GridView1.DataKeys[gRow.RowIndex]["ID"];
Debug.Print("PK ID = " + PKID);
Debug.Print("Row index click = " + gRow.RowIndex);
Label VendorName = (Label)gRow.FindControl("VendorName");
txtVendor.Text = VendorName.Text;
}
So you don't have to use row command. But, you can drop in a plain jane button, or image button - just wire up a plain old regular event. You can pick up the current row as per above, and you off to the races.

Related

problem handling Gridview_SelectIndexChanging event with an UpdatePanel ASP.NET

i will try to explain what i have, what i'm trying to do and what is my problem
what i have:
i have a modal with and update panel (i need it because the postback closes my modal) that has two text box, a button and a gridview.
in the textbox i write the name and the surname of a person and with the button i retrieve the data and put it in a gridview.
the gridview has a control to select the row that i want, with that row, i use the data from first three cells to change some hidenField values to use in another function.
in the bottom of the modal i have the add button that only changes a textbox to show that i have the correct data. that button is not enabled by default and the event must enable it
what i want:
i want to select the row that i need, enable the add button and fetch the data in my textbox.
where is my problem:
when i select my row, selectedindexchanging fires but nevers enables the add button so cant fetch my textbox for using it
code:
front code:
<div class="modal fade" id="modalSocio" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabelSocio">
<asp:Label ID="Label4" runat="server" CssClass="text-primary"></asp:Label>
</h5>
</div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<div class="modal-body">
<div class="row g-3">
<div class="row">
<div class="col-3">
<asp:TextBox ID="txtBusquedaNombre" runat="server" CssClass="form-control" placeholder="Nombre"></asp:TextBox>
</div>
<div class="col-3">
<asp:TextBox ID="txtBusquedaApellido" runat="server" CssClass="form-control" placeholder="Apellido"></asp:TextBox>
</div>
<div class="col-3">
<asp:LinkButton ID="btnBuscarSocio" runat="server" CssClass="btn btn-outline-success" Text="Buscar" CausesValidation="false" ToolTip="Buscar" OnClick="btnBuscarSocio_Click" ><span class="fas fa-search"></span></asp:LinkButton>
</div>
</div>
<div class="table-responsive mt-3">
<asp:GridView ID="gvSocios" runat="server" CssClass="table table-bordered" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanging="gvSocios_SelectedIndexChanging" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="Legajo" HeaderText="Nro. Socio" ></asp:BoundField>
<asp:BoundField DataField="nombreSocio" HeaderText="Nombre" />
<asp:BoundField DataField="Apellido" HeaderText="Apellido" />
<asp:CommandField ButtonType="Link" HeaderText="Seleccionar" ShowSelectButton="True" SelectText="<i class='fa fa-check-circle'></i>">
<ControlStyle CssClass="btn btn-outline-secondary" />
</asp:CommandField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
<EmptyDataTemplate>
<div class="alert alert-primary" role="alert">
No se encontraron registros!
</div>
</EmptyDataTemplate>
</asp:GridView>
</div>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
<div class="modal-footer">
<asp:Button ID="BtnCancelarSocio" runat="server" Text="Cancelar" CssClass="btn btn-secondary" OnClick="btnCancelar_Click" CausesValidation="False" />
<asp:Button ID="BtnAgregarSocio" ClientIDMode="Static" runat="server" CausesValidation="false" Text="Seleccionar" CssClass="btn btn-success" OnClick="BtnAgregarSocio_Click" />
</div>
</div>
</div>
</div>
codeBehind:
protected void BtnAgregarSocio_Click(object sender, EventArgs e)
{
TxtSocio.Text = hfidNombreSocio.Value;
}
protected void gvSocios_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
var row = gvFormasPago.Rows[e.NewSelectedIndex];
BtnAgregarSocio.Enabled = true;
hfSocio.Value = row.Cells[0].Text;
hfidNombreSocio.Value = row.Cells[0].Text + " - " + row.Cells[1].Text + " " + row.Cells[2].Text;
}
i tried to not use the enabled attribute for test but when the click event fires the hfidNombreSocio value in that moment is empty and the modal never closes.
maybe i'm not using the update panel right.
the text box code:
<div class="row">
<div class="col-md-4">
<asp:Label ID="lblSocio" runat="server" Visible="false" Text="Socio Cuenta Corriente" CssClass="form-label"></asp:Label>
<asp:TextBox ID="TxtSocio" runat="server" text="0" CssClass="form-control" ></asp:TextBox>
<asp:LinkButton ID="btnBuscar" runat="server" Visible="false" CssClass="btn btn-outline-success" Text="Buscar" CausesValidation="false" ToolTip="Buscar" OnClick="btnBuscar_Click" ><span class="fas fa-search"></span></asp:LinkButton> <%-- this button open the modal --%>
</div>
Ok, folks, this is one of those I wish someone corrected me!!
I am MOST HAPPY to have big buckets of egg on my face.
I have for some time preached that when you pop a dialog, you can't have post backs in that dialog. I stand MUCH corrected!!!!
So, lets build a grid, it allows a search, and then we can click on (select the row).
We have this:
<div id="hoteldialog1" style="display:normal">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
Enter City Name: <asp:TextBox ID="txtSearchCity" runat="server"></asp:TextBox>
<asp:Button ID="cmdSearchCity" runat="server" Text="Search"
style="margin-left:25px" CssClass="btn" OnClick="cmdSearchCity_Click"/>
<br />
<asp:GridView ID="GridHotels" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" OnSelectedIndexChanged="GridHotels_SelectedIndexChanged"
CssClass="table">
<Columns>
<asp:BoundField DataField="HotelName" HeaderText="HotelName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Province" HeaderText="Province" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:Button ID="cmdHSel" runat="server" Text="Select"
CommandName="Select"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
So, when we run, we get/see this:
Now, in above I DID use CommmandName = "select" because I wanted the select row to work. (and I beyond hate all those extra templates). So I used this code to highlight the row.
protected void GridHotels_SelectedIndexChanged(object sender, EventArgs e)
{
// user selected this row - highlight it
int RowIX = GridHotels.SelectedIndex;
GridViewRow gRow = (GridViewRow)GridHotels.Rows[RowIX];
if ((ViewState["MySel"] != null) && ((int)ViewState["MySel"] != RowIX))
{
GridViewRow gLast = GridHotels.Rows[(int)ViewState["MySel"]];
gLast.CssClass = "";
}
gRow.CssClass = "alert-info";
ViewState["MySel"] = RowIX;
}
Ok, so far so simple. And the search button in above is this:
protected void cmdSearchCity_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (SqlCommand cmdSQL = new SqlCommand("SELECT * FROM tblHotels ", conn))
{
if (txtSearchCity.Text != "")
{
// filter grid by city
cmdSQL.CommandText += " WHERE City = #City";
cmdSQL.Parameters.Add("#City", SqlDbType.NVarChar).Value = txtSearchCity.Text;
}
conn.Open();
cmdSQL.CommandText += " ORDER BY HotelName";
GridHotels.DataSource = cmdSQL.ExecuteReader();
GridHotels.DataBind();
}
}
}
Again, really simple.
Ok, now the money shot. Now the Rosetta stone. Now the beef, now the magic, now the amazing!!!
The above as noted was placed in a "div", and has a update panel.
So, now lets drop in another grid!!!
We going to display some people, and pop the above hotel selector grid!!
So, we now have this:
<asp:GridView ID="GPeople" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" >
<Columns>
<asp:BoundField DataField="Firstname" HeaderText="Firstname" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Province" HeaderText="Province" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="cmdGetHotel" runat="server" Text="Hotels"
OnClick="cmdGetHotel_Click"
OnClientClick="return mypop(this);"
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadPeople();
}
void LoadPeople()
{
GPeople.DataSource = MyRst("SELECT * from People Order by FirstName");
GPeople.DataBind();
}
So we now have a grid of people. We now want to on a row click, pop up the hotel list.
So, I suggest jQuery.UI, as is much better (and cleaner then bootstrap dialog).
So we have this row click in above:
<asp:Button ID="cmdGetHotel" runat="server" Text="Hotels"
OnClick="cmdGetHotel_Click"
OnClientClick="return mypop(this);"
/>
So in above, we have both a server side button, and then a client side js function that pops up the dialog (and ONLY returns true if we hit ok.
So, now all we need is the js code to pop up the dialog. That code is this:
<script>
selok = false;
function mypop(btn) {
if (selok)
return true;
var mydiv = $("#hoteldialog1")
mydiv.dialog({
modal: true, appendTo: "form",
title: "Test dialog", closeText: "",
width: "40%",
position: { my: 'left top', at: 'right bottom', of: btn },
buttons: {
Ok: (function () {
selok = true;
btn.click()
}),
Cancel: (function () {
mydiv.dialog("close")
})
}
});
return false;
}
</script>
(set hotel grid display:none - jquery.ui will manage this).
So, now we get this:
So we are free to search, have post-backs, and of course select the row.
If the user hits ok, then the first grid button code runs. I did NOT use the selected index change event for that first grid (I could have), but I used this code:
protected void cmdGetHotel_Click(object sender, EventArgs e)
{
Debug.Print("hotel grid click");
Button btn = (Button)sender;
GridViewRow gRow = (GridViewRow)btn.Parent.Parent;
Debug.Print("Grid row = " + gRow.RowIndex);
Debug.Print("PK = " + GPeople.DataKeys[gRow.RowIndex]["ID"].ToString());
Also note, that while the pop grid is hidden? We can STILL get any data from that row, since the selected index of that grid does persist!!
eg:
GridViewRow gRow = (GridViewRow)GHotels.Rows[GHotels.SelectedIndex];
Debug.Print("Grid row = " + gRow.RowIndex);
Debug.Print("PK = " + GPeople.DataKeys[gRow.RowIndex]["ID"].ToString());
// cells collection, or find control to grab any other value
So, I think you should dump the bootstrap dialog.
Put your 2nd nice grid + searching, post backs - what ever you want into a div with a name, and use jQuery.UI to pop the grid. This will allow searching, post backs and then when the user selects the row and hits ok, then you are now free to close this dialog, and you have "index" of that pop set for you.

how to get values from gridview in asp.net web forms?

hello all i am new to asp.net and i want to get values from grid view when i click on Edit or Delete or and i don't know how to do it i am sending data table to show table data and i don't want to use SQL Data Source from Visual tool box.
i have tried the following code but it gives error
protected void dgv1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
int rowindex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex;
dgv1.EditIndex = rowindex;
dgv1.DataBind();
}
}
}
my view page code is as follows
<form id="form1" class="form-control form-group" runat="server">
<asp:Panel ID="pnlAddCustomers" runat="server" Height="376px">
<asp:Label ID="lblAddCustomers" runat="server" Text="Add Customers"></asp:Label>
<br runat="server"/>
<asp:Label ID="labelAddName" Text="Customer Name" runat="server"></asp:Label>
<asp:TextBox ID="txtboxcustomername" runat="server" Height="16px"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text=" Customer City "></asp:Label>
<asp:TextBox ID="txtboxcity" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text=" Customer Telephone "></asp:Label>
<asp:TextBox ID="txtboxtelephone" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Add Customers" Height="57px" Width="208px" OnClick="Button1_Click" />
</asp:Panel>
<asp:Panel ID="PnlViewCustomers" CssClass="table-dark" runat="server" Height="408px" >
<asp:SqlDataSource ID="SqlDataSource1" runat="server"></asp:SqlDataSource>
<asp:GridView ID="dgv1" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" EnableTheming="True" PageSize="5" Width="100%" OnRowCommand="dgv1_RowCommand">
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
</asp:Panel>
</form>
and body pic is as follow
enter image description here
Try this:
LinkButton lnkBtn = (LinkButton)e.CommandSource; // the button
GridViewRow gvRow = (GridViewRow)lnkBtn.Parent.Parent; // the row
You may also get the data key index by adding DataKeyNames="MyTableKeyID" to the <asp:GridView /> tab, then
string selectedKeyIndex = this.GridView1.DataKeys[myRow.RowIndex]["MyTableKeyID"].ToString();

Cant access textbox inside gridview inside modalpopup

I have the below a gridview inside a modal popup, I use update a textbox inside an edititemtemplate,then use linkbutton to trigger a postback event so I can save the value inside the textbox for that gridview row,but at code bhind I cant access the textbox when I use findcontrol.
Please assit
<div id="userForm" class="form-horizontal" style="display:none;width:100%;border:solid 0px;" >
<input ID="btnshowRun" type="button" Value="Show Leave" class="btn btn-warning " style="float:lefts;" OnClick ="showhide();"/>
<asp:UpdatePanel ID="UpdatePanel5" runat="server" style="display:none;">
<ContentTemplate>
<asp:GridView ID="GridView2" runat="server" AllowSorting="False"
AutoGenerateColumns="False" DataKeyNames="EntryID"
EmptyDataText="There are no data records to display."
ShowFooter="False" HorizontalAlign="Center" OnRowCommand="gvl_RowCommand"
ShowHeaderWhenEmpty="True" CssClass="table table-striped table-bordered table-hover table-responsive table-condenseds " Width="100%" GridLines="None">
<Columns>
<asp:TemplateField HeaderText="Total" SortExpression="Total">
<EditItemTemplate>
<asp:TextBox ID="Tot" runat="server" Text='<%# Bind("Total") %>' class="form-control"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Total") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="CommandButton" runat="server" CommandArgument='<%# Eval("EntryID") %>' UseSubmitBehaviour=true CausesValidation="False"
CommandName="Update" OnCommand="Updates_Command" Text="Update" class="btn btn-primary btn-sm contrl" Style="margin-right: 10px;"
/>
<asp:LinkButton ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" class="btn btn-warning btn-sm contrl" Style="margin-right: 10px;"
Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="Button1" runat="server" CommandArgument='<%# Eval("EntryID") %>' class="form-control btn btn-default btn-blocks contrl btn-sms"
CommandName="Edit" OnCommand="Button1_Command" Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Center" Font-Bold="True" ForeColor="Black"></HeaderStyle>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
This is how am accessing the control in C#
protected void Updates_Command(object sender, CommandEventArgs e)
{
Control p = ((Control)sender).Parent;
int tot = int.Parse(((TextBox)p.FindControl("Tot")).Text);
int rowid = int.Parse(Session["RowID"].ToString());
int suc = Database.ExecuteNonQuery("UPDATE LeaveAssignmentEntry SET Total='" + tot + "' WHERE Id = " + rowid + "");
if (suc==1)
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('Successful')", true);
}
}
you can use this code to find control inside template :
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = GridView2.Rows[index];
TextBox tot = (TextBox)selectedRow.FindControl("Tot");
Use below line to get text from TextBox with FindControl method:
string tot = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("Tot")).Text;

Update progress control not showing image using TextChanged Event

I want to show Loading image while TextChanged Event because I am filling gridview while TextChanged Event change. I am trying but image is not showing.
Below is my design code:
<asp:Panel ID="pnl_Candidate" runat="server" Visible="false">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="txt_CandidateSource" runat="server" OnTextChanged="txt_CandidateSource_TextChanged" AutoPostBack="True" class="form-control input-sm" placeholder="Candidate Source" Width="30%" />
<asp:GridView ID="GridView1" runat="server" CssClass="footable" Style="max-width: 500px" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="true" PageSize="5" DataKeyNames="EMail" >
<Columns>
<asp:TemplateField HeaderText="Sno">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="CandidateSource" HeaderText="CandidateSource"></asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name"></asp:BoundField>
<asp:BoundField DataField="Gender" HeaderText="Gender"></asp:BoundField>
<asp:BoundField DataField="DOB" HeaderText="DOB"></asp:BoundField>
<asp:BoundField DataField="Mobile" HeaderText="Mobile"></asp:BoundField>
<asp:BoundField DataField="EMail" HeaderText="EMail"></asp:BoundField>
<asp:BoundField DataField="Qualification" HeaderText="Qualification"></asp:BoundField>
<asp:BoundField DataField="CurrentLocation" HeaderText="CurrentLocation"></asp:BoundField>
<asp:BoundField DataField="TotalExperience" HeaderText="TotalExperience"></asp:BoundField>
<asp:BoundField DataField="CurrentCompany" HeaderText="CurrentCompany"></asp:BoundField>
<asp:BoundField DataField="Designation" HeaderText="Designation"></asp:BoundField>
<asp:BoundField DataField="WorkLocation" HeaderText="WorkLocation"></asp:BoundField>
<asp:TemplateField HeaderText="add Comment Status">
<ItemTemplate>
<asp:LinkButton ID="lnkbtn_Comment_Status" runat="server"
Text="Comment" OnClick="lnkbtn_Comment_Status_Click"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="pgr" />
<EmptyDataTemplate>
<center>
<asp:Label ID="Label2" CssClass="Helptext_red" runat="server" Text="No Candidate Available"></asp:Label>
</center>
</EmptyDataTemplate>
<HeaderStyle CssClass="header" />
<AlternatingRowStyle CssClass="alt" />
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress2" DisplayAfter="0" runat="server" AssociatedUpdatePanelID="UpdatePanel1">
<ProgressTemplate>
<div class="modal">
<div class="center">
<img alt="" src="loading_bar_animated.gif" />
</div>
</div>
</ProgressTemplate>
</asp:UpdateProgress>
My TextChange Event and Filling gridview in every text change event:
protected void txt_CandidateSource_TextChanged(object sender, EventArgs e)
{
if (txt_CandidateSource.Text != "")
{
System.Threading.Thread.Sleep(2000);
DataTable dt = new DataTable();
bo.Para1 = txt_CandidateSource.Text;
bo.Para2 = txt_Qualification.Text;
bo.Para3 = txt_CurrentLocation.Text;
bo.Para4 = txt_TotalExperience.Text;
bo.Para5 = txt_Designation.Text;
dt = bl.Search_CandidateMaster(bo);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
use this UpdateProgress fine work
CSS:
<style type="text/css">
.rcs {
display: block;
position: absolute;
top: 40%;
left: 45%;
}
</style>
ASPX:
<asp:UpdateProgress ID="UpdateProgress1" runat="server" DisplayAfter="1" DynamicLayout="true">
<ProgressTemplate>
<asp:Panel ID="Panel1543633" runat="server" Style="width: 100%; height: 100%; position: fixed; left: 0; top: 0; z-index: 99999; background: rgba(0, 0, 0, 0.2);">
<div class="rcs">
<asp:Image ID="Image1" ImageUrl="~/images/loading.gif" AlternateText="Processing"
runat="server" />
</div>
</asp:Panel>
</ProgressTemplate>
</asp:UpdateProgress>

Avoid page refresh when clicking on LinkButton in GridView

I have a GridView control, and inside of this control I have defined a link button using GridView.ItemTemplate.
I am using this to open a new window on click. When I click on the link button, however, the page refreshes before opening the new window.
How can I stop the page from refreshing after the link button is clicked?
HTML
<asp:UpdatePanel ID="UpdatePanel4" runat="server">
<ContentTemplate>
<asp:GridView ID="DataGrid1" style="visibility:visible" runat="server" AlternatingRowStyle-BackColor="#E9EDF5" Font-Names="Arial"
ForeColor="#09538A" Font-Size="12px" BackColor="#ffffff" BorderColor="DarkGray" Font-Bold="true"
HeaderStyle-BackColor="#298DC7" EnableViewState="false" CellSpacing="20"
CellPadding="10" HeaderStyle-Font-Bold="true" AutoGenerateColumns="False" OnRowCommand="DataGrid1__RowCommand" OnRowDataBound="DataGrid1__RowDataBound" >
<HeaderStyle Font-Names="Arial;" CssClass="MyHeaderStyle" Font-Size="13px" ForeColor="White" Font-Bold="True" Height="20" BackColor="#298DC7" ></HeaderStyle>
<asp:templatefield headertext="NDC" ItemStyle-CssClass="col" ItemStyle-HorizontalAlign="Justify" HeaderStyle-Width="10%" ItemStyle-Width="10%"> <Columns>
<itemtemplate>
<asp:linkbutton id="productcode" ForeColor="#09538A" runat="server" text='<%#Eval("product code")%>'></asp:linkbutton>
</itemtemplate>
</asp:templatefield>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<AjaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" BackgroundCssClass="modalBackground" CancelControlID="cancel" TargetControlID="Button3" runat="server" PopupControlID="pnlpopup"> </AjaxToolkit:ModalPopupExtender>
<asp:Button ID="Button3" runat="server" style="visibility:hidden" Text="Button" />
<asp:Panel ID="pnlpopup" CssClass="PanelPopup" runat="server">
<div style="width:inherit;text-align:center;">Products of <%=ndc %> Product Family</div>
<asp:GridView ID="GridView1" runat="server" AlternatingRowStyle-BackColor="#f1f4f8" Width="980px" Font-Names="Arial"
Font-Bold="True" ForeColor="#09538A" Font-Size="13px" BackColor="#ffffff" BorderColor="DarkGray"
HeaderStyle-BackColor="#99cccc" EnableViewState="false" CellSpacing="0" style="padding:10px;"
CellPadding="3" ShowFooter="false" AllowPaging="True" AutoGenerateColumns="False" OnRowDataBound="productInfo_RowDataBound" >
<HeaderStyle Height="10%" CssClass="MyHeaderStyle" Font-Size="13px" ForeColor="#FFFFFF" Font-Bold="true" BackColor="#298DC7"></HeaderStyle>
<rowstyle Height="20px" />
<alternatingrowstyle Height="20px"/>
<Columns>
<asp:boundfield datafield="product code" sortexpression="customers " ItemStyle-CssClass="col" headertext="NDC"/>
<div id="div<%# Convert.ToString(Eval("customer"))+ Convert.ToString(Eval("ManufacturingPartner"))+ Convert.ToString(Eval("product code"))+ Convert.ToString(Eval("Sales Person")) %>" style="display: none; position: relative; left: 15px; overflow: auto">
<asp:GridView ID="gvOrderInfo" runat="server" ForeColor="#09538A" AutoGenerateColumns="false" BorderStyle="Double" BorderColor="#df5015" Width="500px" OnRowDataBound="gvOrderInfo_RowDatabound">
<HeaderStyle CssClass="MyHeaderStyle" Font-Size="13px" ForeColor="#FFFFFF" Font-Bold="True" BackColor="#298DC7"></HeaderStyle>
<RowStyle BackColor="#E1E1E1" />
<AlternatingRowStyle BackColor="White" />
<HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
<Columns>
<asp:BoundField DataField="Order Number" HeaderText="Order Number" ItemStyle-Width="75px" ItemStyle-CssClass="col" HeaderStyle-HorizontalAlign="Left" />
</Columns>
</Columns>
</asp:GridView>
</div>
</asp:GridView>
</asp:Panel>
Codebehind
protected void DataGrid1__RowDataBound(Object sender, GridViewRowEventArgs e)
{
this.UpdatePanel4.Update();
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnk = e.Row.FindControl("ppp") as LinkButton;
lnk.Click += new EventHandler(link_Click);
//ScriptManager.GetCurrent(this).RegisterPostBackControl(lnk);
// string a = String.Format("0:N}",Convert.ToDecimal(e.Row.Cells[3].Text));
if (e.Row.Cells[0].Text != "Total")
{
//M1-Fmodification starts from here
if (ListBox2.Items.Count > 0)
//if (DataGrid1.Columns[0].Visible == true)
{
}
}
}
}
I would not use a LinkButton, but a straight html link.
<ContentTemplate>
<a href="#" ID='<%#Eval("product code")%>' onclick="doSomething();">
<%#Eval("productcode")%>
</a>
</ContentTemplate>
And then you can define some javascript in the page.
function doSomething(ProductCode) {
// enter the code here to open the window
}
Also, I would stay away from using UpdatePanel, but that's just my preference.

Categories

Resources