I Have a user control as a grid view.
I am adding it dynamically...i have made the method for data source but i am unable to use others events of grid view like "Row_Data_Bound" etc.
I have seen the code on net which says create delegates and add following
protected void Simple_DGV_RowDataBound(object sender, GridViewRowEventArgs e)
{
OnRowDataBound(e);
}
but i get an error here saying The name OnRowDataBound does not exist in the current context
Can anyone help me out with accessing the events of user control grid view in the parent page
EDIT:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl1.ascx.cs" Inherits="ProjectManagement.UserControl.User1" %>
<div>
<asp:GridView ID="ucGridView" runat="server" SkinID="gvSchedulerSkin" AllowSorting="false" OnRowDataBound="ucGridView_RowDataBound">
<RowStyle Width="20px" />
</asp:GridView>
</div>
this is my ascx page...i want to use this grid view at more than one place(at run time) so i have created a user-control...
Basically i want to call this user control from my code behind and then using its rowdatabound i want to data to be bind with the gridview..
i saw on websites it says use events bubbling...but i do not know how to implement that.
so can u help in that matter..so that i can use rowdatabound normally as i do
thnx in advance
I've created a project locally and this is ok.
I've a control called TestUserControl.ascx. I dragged a GridView control onto the user control in design mode and called it "grdControlsGrid".
This generated the below mark up.
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs" Inherits="TestASPNet.TestUserControl" %>
<asp:GridView ID="grdControlsGrid" runat="server">
</asp:GridView>
Then I added the event OnRowDataBound by typing "OnRowDataBound=" to the hmtl after runat="server". When you hit equals it gives you the option to create the method for this event. Double click the "Create method" option and this will wire up the event to the method for you.
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="TestUserControl.ascx.cs" Inherits="TestASPNet.TestUserControl" %>
<asp:GridView ID="grdControlsGrid" runat="server" OnRowDataBound="grdControlsGrid_OnRowDataBound">
</asp:GridView>
This code now resides in your user control as per the code below.
Alternatively you can wire the event up yourself in the users controls load.
public partial class TestUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
//Manually Add event handler
grdControlsGrid.RowDataBound += GrdControlsGrid_RowDataBound;
}
private void GrdControlsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
//Manually bound event
}
protected void grdControlsGrid_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
//Auto wired event
}
}
for some reason when auto wiring via the markup you get the event "OnRowDataBound".. but when done in the code behind manually you get "RowDataBound". my guess is that they are one of the same.. but maybe someone else can shed light on that.
Hope that helps.
Related
ASPX code :
<asp:Button ID="medicalSub" runat="server" ValidationGroup="medical" Text="Save" CausesValidation="false" UseSubmitBehavior="false" ClientIDMode="Static" OnClick="medicalSub_Click" />
ASPX.CS code :
protected void medicalSub_Click(object sender, EventArgs e)
{
console.writeline("hello");
}
Error
According to that stack trace, the problem is not calling the click method. Instead, it's failing trying to load viewstate for a dropdown list control. This is long before trying to handle any events.
Try this:
copy and paste this in your page load section of your code and check if that helps:
If(!IsPostBack)
{
}
Or the following will hep:
<%# Page so just add the rest => EnableEventValidation="false" %>
Product.aspx
<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:textbox runat="server" ID="quantitytb"></asp:textbox>
<asp:Button CssClass="addtocart-button" runat="server" Text="Add to cart" ID="addtocartbutton" onclick="addtocartbutton_Click"></asp:Button>
</ItemTemplate>
</asp:DataList>
Product.aspx.cs
protected void addtocartbutton_Click(object sender, EventArgs e)
{
quantitytb.Text="1";
}
Line 1 of Product.aspx
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Product.aspx.cs" Inherits="Product" %>
Above is just a small section of how my code looks like. Any controls I add to my Product.aspx page does not work in the .cs file. There will be an error saying "The name 'control name' does not exist in the current context". Literally tried all the solutions I can find online but to no avail.
Note that I am using ASP.Net Empty Web Site and not Web Application so there's no designer.cs file.
You can't access quantitytb directly because it is within a DataList. Similar to any data-bound container (gridview, repeater, formview, etc), you must target a specific item/row to find its child controls. If your datalist has 10 items in it, that means you'll have 10 occurrences of quantitytb - if you don't specify which one you're targeting, the code will throw an error.
If you're trying to modify the textbox that's the sibling of the clicked button, perhaps what you're looking for is this:
protected void addtocartbutton_Click(object sender, EventArgs e)
{
//Find the button that was clicked
Button addToCart = (Button)sender;
//Get the button's parent item, and within that item, look for a textbox called quantitytb
TextBox quantitytb = (TextBox)addToCart.Parent.FindControl("quantitytb");
//Set that textbox's text to "1"
quantitytb.Text="1";
}
I am trying to change the text on a label from a custom event. When I update the text through a button click event (Button1_Click), everything works. However, when I try to update the same label in a custom event(ArReceiver_OnArDataUpdate) nothing happens. ArReceiver_OnArDataUpdate is getting triggered properly.
Why does my event react differently than a button click event? What do I need to do to get my event working?
This is an out of the box web project in VS2013. Here's the code to explain better:
Code Behind:
namespace WinReceiver
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
ArReceiver.OnArDataUpdate += ArReceiver_OnArDataUpdate;
}
void ArReceiver_OnArDataUpdate(object sender, ArEventArgs e)
{
labelVoltage.Text = "Reset";
UpdatePanel1.Update();
}
protected void Button1_Click(object sender, EventArgs e)
{
labelVoltage.Text = "Button Clicked";
UpdatePanel1.Update();
}
}
}
Default.aspx: (The site.master has the ScriptManager in it by default)
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="labelVoltage" runat="server" Text="..."></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
EDIT:
I did find this question that says it's not possible, but can someone explain why this may not be possible? I'm ok with a page refresh too, but when I tried Respone.Redirect I got exceptions.
If OnArDataUpdate is a static event, it's likely to be fired when the ASP.NET page rendering process is out of scope, and thus the label is not existent because it isn't in scope of the page being processed (since ASP.NET is stateless).
However, if ArReceiver is a control on the page, it should process fine, and the issue could be within the control or something overriding it (depending on when the event fires, it may get overridden by viewstate).
Below is my code:
default.aspx:
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master"
AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="TestAjax._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
//I set an onClick event on this button:
< input type = "button" id="btnCl" onclick = "doJob" />
</asp:Content>
default.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
//fires on page load;
}
void doJob()
{
//code here;
}
The question is:
Why didn't the onclick event trigger? (On default.aspx btnCL)
Thanks
You need to use an asp:Button control like this:
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
and then have the event method like this:
protected void Button1_Click(object sender, EventArgs e)
{
// your code here
}
When using a regular input tag, onclick is for javascript, you have to instead use the ASP.NET control asp:Button in order to hook up the C# method.
As stated in my comment in OP your current method is making a client-side call (so looking for a JavaScript function called doJob. You need to make a server-side call so need to use an <asp:Button> control. Some example of how you could achieve this:
Web-Page (.aspx)
<asp:Button ID="btnDoJob" Runat="server" Text="Do Job" OnClick="btnDoJob_Click" />
Code-Behind (.CS)
protected void btnDoJob_Click(object sender, EventArgs e)
{
// Do your action here...
}
Because dojob() is a server-side function, but onclick on that input element is a client-side event. You're probably getting an error in your JavaScript console saying that dojob() is an undefined function.
Use an asp:Button instead of an input to make use of server-side click events. Also, dojob() should be protected. By not declaring a protection level I think the default is private so the page controls might not even be able to see it. It should match the event handler for a button click:
protected void dojob(Object sender, EventArgs e)
{
}
I am writing a card game app using Ajax, c# and .NET 3.5. Due to the nature of the interface I have numerous update panels that Im trying to manage and update across various user action. I'm having problems with one though.
The players current hand is built by binding a list of Card objects to a repeater and then dynamically creating a Card UserControl and adding it to the Controls of a PlaceHolder when each item is databound. The code is roughly as follows:
On the page
<asp:UpdatePanel ID="pnlInHand" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="rptInHand" runat="server" onitemdatabound="rptInHand_ItemDataBound">
<ItemTemplate>
<asp:PlaceHolder ID="plcInHandCard" runat="server" />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
In code behind
protected void rptInHand_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Card card = (Card)e.Item.DataItem;
PlaceHolder plcCard = (PlaceHolder)e.Item.FindControl("plcInHandCard");
plcCard.Controls.Add(CreateCardControl());
}
private CardControl CreateCardControl()
{
CardControl cardControl = (CardControl)Page.LoadControl("~/UserControls/CardControl.ascx");
//Set control properties here
return cardControl;
}
The Card Control includes a Button. The ClickEvent for this button calls a Method of the Parent Page that needs to update a seperate UpdatePanel as well as remove the card Control from the Panel that it is sitting within.
I have two issues.
When I click the Card Control Button, because it has been created as part of a repeater within an updatePanel, it no longer exists when the page is posted back and so the Click event for the button within the control never fires. I can obviously rebind the repeater on page load, but does this mean I have to essentially do this on every postback?
More importantly I need a way to trigger the update of another updatepanel in the parent page when the Card control's click event is raised. Is there a way of setting a trigger on an update panel that listens out for an event within a dynamicaly loaded UserControl?
Many thanks
Stewart
Sample code from ASP.net site that should address your point 2 problem follows.
I'll leave the translation to your code to you.
I may be misunderstanding what you are trying to do but I believe once you get this working your issue with point 2 is no longer relevant as you'll get the AJAX postback you want from your parent update panel.
Good luck!
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="itemDataBound">
<ItemTemplate>
<mycontrol:user ID="user1" runat="server" OnCausePostBack="user1_CausePostBack" /> <br />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
protected void itemDataBound(object sender, RepeaterItemEventArgs e)
{
ModalPopup_WebUserControl mw=(ModalPopup_WebUserControl)e.Item.FindControl("user1");
AsyncPostBackTrigger at = new AsyncPostBackTrigger();
at.ControlID = mw.ID;
at.EventName = "CausePostBack";
UpdatePanel2.Triggers.Add(at);
}
protected void user1_CausePostBack(object sender, EventArgs e)
{
// do something
}
just an idea for point 2 : what about add a property in the cardControl to set a reference to the updatepanel/s ? from there you can add triggers or call panel.update in the button event
for point one yes u will have to do it. u will have to re create the controls
for point 2 a simple updatePanel.update() would do the job insode of any event if you are using an event that was binded to a dynamically created control then u will have to rebind the event on every page postback