I've tried putting an asp button (with use submit equals false) or an asp textbox that should open the popup pnel (which I've defined), but the popupextender causes a postback and displays all of the current page content (instead of iring the page method I defined).
It's nearly 5 hours of debugging, opening new projects (to try and reproduct the case in other project) and so on.
Thanks in advance...
This is the body of the page that handles the user control:
<body>
<form id="form1" runat="server">
<div>
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"></ajaxToolkit:ToolkitScriptManager>
<ucpop:popup ID="gv" runat="server" />
</div>
</form>
This is the usercontrol:
<asp:UpdatePanel runat="server" ID="upExample">
<ContentTemplate>
<asp:GridView runat="server" ID="gvCars"
OnDataBinding="gvCars_DataBinding"
OnRowCommand="gvCars_RowCommand"
>
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="ibPopup" ImageUrl="~/Resources/Perspective-Button-Go-icon.png" Width="20px" Height="20px" />
<ajaxToolkit:PopupControlExtender ID="pce" runat="server" TargetControlID="ibPopup" DynamicControlID="pToUpdate"
PopupControlID="pToExtend" DynamicContextKey='<%#Eval("id") %>' DynamicServiceMethod="GetDynamicContent">
</ajaxToolkit:PopupControlExtender>
<asp:Panel runat="server" ID="pToExtend" BackColor="Red" style="display:none">Hello
<asp:Panel runat="server" ID="pToUpdate">
</asp:Panel>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lidPopup" Text='<%=Eval("id") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="lNamePopup" Text='<%=Eval("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton CommandName="p" runat="server" ID="ibNotPopUp" ImageUrl="~/Resources/Perspective-Button-Go-icon.png" Width="20px" Height="20px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
This is the Page web codebehind:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string GetDynamicContent(string contextKey)
{
StringBuilder sTemp = new StringBuilder();
sTemp.Append(string.Format("<div>Cool! I'm {0}</div>", contextKey));
return sTemp.ToString();
}
}
This is the UserControl codebehind:
public partial class GridViewWithPopUpControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvCars.DataBind();
}
}
protected void gvCars_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
string t = e.CommandName;
}
}
protected void gvCars_DataBinding(object sender, EventArgs e)
{
GridView gv = (GridView)sender;
gv.DataSource = CarList.CarCollection;
}
}
It's pretty standard code... yet not working as expected. I think of upgrading the ajaxtoolkit, but other features in the web site system might be ruined, so I have to think it over.
Thanks again.
Supposedly, the Page method wasn't in the page but rather in the User Control.
I did not write the User Control nor the page, I just needed to add functionality and found that problem - and fixed it...
Related
On textbox OnTextChanged event the postback cycle triggering twice. Breakpoints on both methods to understand the issue.
Here is my code sample
<form id="form1" runat="server">
<div>
<asp:TextBox runat="server" ID="TextBox1" OnTextChanged="TextBox1_TextChanged" AutoPostBack="true" />
</div>
<asp:Label ID="Label1" runat="server"></asp:Label>
</form>
Its code behind.
public static int cycle { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
cycle++;
Label1.Text = cycle.ToString();
}
Avoid using AUTOPOSTBACK, keep the OnTextChanged event trap
and add a button (hidden or not) to catch the return pression on the textbox
to produce the POSTBACK.
Here is an example
<asp:Panel runat="server" CssClass="col-md-2">
<asp:Panel runat="server" CssClass="form-group input-group" DefaultButton="BTN_Cerca">
<span class="input-group-btn">
<asp:Button runat="server" ID="BTN_Cerca" Text="Codice" CssClass="btn btn-secondary" ToolTip="Cerca in magazzino"/>
</span>
<asp:TextBox runat="server" ID="TXT_Search" CssClass="form-control" placeholder="Numero Articolo" OnTextChanged="TXT_Search_TextChanged" />
</asp:Panel>
</asp:Panel>
Button btnAddrecord = (Button)sender;
GridViewRow gvr =(GridViewRow)btnAddrecord.NamingContainer;
if (btnAddrecord.CommandName == "onAddMaterial")
Define the button in your grid view markup and assign a CommandName value to the button, like this:
<asp:GridView ID="GridView1" AutoGenerateColumns="False" runat="server"
OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Add Record">
<ItemTemplate>
<asp:Button ID="btnAddRecord"
CommandArgument="<%# ((GridViewRow)Container).RowIndex %>"
CommandName="AddRecord" runat="server" Text="Add" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Now in your code-behind, you can handle the OnRowCommand event and AddRecord command, like this:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddRecord")
{
// Get index of row passed as command argument
int index = Convert.ToInt32(e.CommandArgument.ToString());
// Your logic here
}
}
1.Give a command name for button..
2.Then inside gridview
if e.commandname="yourcommandname"
{
//your code..
}
<!--We use onrowcommand for getting the selected row -->
<asp:GridView runat="server" ID="gvTest" AutoGenerateColumns="False" OnRowCommand="gvTest_OnRowCommand" >
<Columns>
<asp:TemplateField HeaderText="BookId" >
<ItemTemplate>
<asp:Label runat="server" ID="lblBookId" Text='<%# Bind("[BookId]") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtBookId" Text='<%# Bind("[BookId]") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:TemplateField>
<asp:TemplateField HeaderText="Options">
<ItemTemplate>
<asp:Button runat="server" ID="btnDelete" CommandArgument="<%# Container.DisplayIndex %>" CommandName="IsDelete" Text="Delete"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
//Code Behind
protected void gvTest_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
//getting rowindex which we have selected by using CommandArgument
int rowindex = Convert.ToInt32(e.CommandArgument);
if (e.CommandName == "IsDelete")
{
int bkid = gvTest.Rows[rowindex].Cells[0].FindControl("BookId");
//call a method to delete book using the bkid
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
I am using Entity Framework with ASPX webform. In my GridView(GV) I make all my columns with ItemTemplates and EditTemplates. When in edit mode I can select a new value, but it does not update the record. In the GV I have a DropDownList which is set to a EntityDataSource that matches it's related table for that field. What steps do I need, what events do I need to handle? I have tried the RowEditing and RowUpdating events, but have no useful code thus far. If you want me to show you some bad code - just ask and I will be more than happy too. I just need some guidance on wiring this up.
Assuming I have an ADO.NET entity data model called - customerEntities which has one table Customers with 3 columns:
CustomerId
Name
Surname
ASPX:
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateEditButton="true"
AutoGenerateColumns="false" onrowcancelingedit="gvCustomers_RowCancelingEdit"
onrowediting="gvCustomers_RowEditing" onrowupdating="gvCustomers_RowUpdating" DataKeyNames="CustomerId">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Bind("CustomerId") %>' />
<asp:Label ID="lblName" runat="server" Text='<%# Bind("Name") %>' />
<asp:Label ID="lblSurname" runat="server" Text='<%# Bind("Surname") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' />
<asp:TextBox ID="txtSurname" runat="server" Text='<%# Bind("Surname") %>' />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindCustomers();
}
private void BindCustomers()
{
customerEntities entityModel = new customerEntities();
gvCustomers.DataSource = entityModel.Customers;
gvCustomers.DataBind();
}
protected void gvCustomers_RowEditing(object sender, GridViewEditEventArgs e)
{
gvCustomers.EditIndex = e.NewEditIndex;
BindCustomers();
}
protected void gvCustomers_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvCustomers.EditIndex = -1;
BindCustomers();
}
protected void gvCustomers_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int customerId = (int)gvCustomers.DataKeys[e.RowIndex].Value;
TextBox txtName = (TextBox)gvCustomers.Rows[e.RowIndex].FindControl("txtName");
TextBox txtSurname = (TextBox)gvCustomers.Rows[e.RowIndex].FindControl("txtSurname");
customerEntities entityModel = new customerEntities();
Customer customer = entityModel.Customers.Where(c => c.CustomerId == customerId).First();
customer.Name = txtName.Text;
customer.Surname = txtSurname.Text;
entityModel.SaveChanges();
gvCustomers.EditIndex = -1;
BindCustomers();
}
I have a List<Collection<string>>object with 10,000 objects in it and I want to show those strings as report(in a grid view), but binding the object directly to grid produces me no result.So can anyone help me out as to how to bind the collection of strings as different columns with the header name that I need.
You may use [] index to bind the dataSource (List of string/array) item.
Markup:
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal
ID="Literal1"
runat="server"
Text='<%#Eval("[0]") %>'
>
</asp:Literal>
<asp:Literal
ID="Literal2"
runat="server"
Text='<%#Eval("[1]") %>'
>
</asp:Literal>
<asp:Literal
ID="Literal3"
runat="server"
Text='<%#Eval("[2]") %>'
>
</asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<List<string>> list = new List<List<string>>()
{
new List<string>() {"A","B","C" },
new List<string>() { "P","Q","R"}
};
GridView1.DataSource = list;
GridView1.DataBind();
}
}
I am using a gridview. When I click on the edit button the update and cancel button appear.
Upon modifying the values in textbox which come from EditItemTemplate , the new values dont show in the event handler rowupdating(), instead I get the values which appear when the page was rendered. How do I grab the new values from these textboxes and proceed further?
Here is the code.
<asp:GridView ID="GridView1" runat="server" AutoGenerateEditButton="true" AutoGenerateColumns="false"
AutoGenerateDeleteButton="true" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblId" runat="server" Text='<%# Eval("id") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtId" runat="server" Text='<%# Eval("id") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("cpuname") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("cpuname") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server" Text='<%# Eval("status") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtStatus" runat="server" Text='<%# Eval("status") %>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox text = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");
}
You should use the two way binding here. I.e. Bind instead of Eval:
<asp:TextBox ID="txtId" runat="server" Text='<%# Bind("id") %>'></asp:TextBox>
Here is the link to documentation:
Data-Binding Expressions Overview
Here is my code behind:
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("Data");
for(int i = 0; i < 20; i++)
table.Rows.Add(new object[] { i });
GridView1.DataSource = table;
if(!IsPostBack) // <<<<<<<<<<<<
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox text = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtId");
}
If you have written the code to bind the grid in page load ensure that it is enclosed in if(!IsPostBack)
I hope it does help
Try this
TextBox text = (TextBox)GridView1.Rows[e.EditIndex].FindControl("txtName");
This ended up working for me.
protected void MyGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string value = e.NewValues[0].ToString();
// ...
}
i'm follow platon and it's work!
Put if(!IsPostBack) before databind(),
and insert GridView1.DataBind(); after GridView1.EditIndex = e.NewEditIndex; in GridView1_RowEditing