Setting my TextBox Text value isn't doing anything - c#

In my webform I have a few textboxes. I've got an event handler for one of my web user controls. When the event is fired, I set the textbox value but nothing happens.
It doesn't look like the page is posting back (does it have something to do with my web user control in a modal popup?) which I assume is the problem...
Am I doing something wrong?
Webform Event Handler:
protected void Page_Load(object sender, EventArgs e)
{
SearchCompanies1.CompanyFound += new WebParts_SearchCompanies.CompanyFoundEventHandler(SearchCompanies1_CompanyFound);
}
void SearchCompanies1_CompanyFound(Company company)
{
myTextBox.Text = company.Name;
popup.Hide();
}
Modal popup and panel:
<cc1:ModalPopupExtender ID="popup" runat="server" DropShadow="true"
TargetControlID="lnkSearchEditCompany" PopupControlID="pnlSearch"
BackgroundCssClass="modalBackgroundSearchCompany" CancelControlID="lnkCancel">
</cc1:ModalPopupExtender>
<asp:Panel ID="pnlSearch" runat="server" CssClass="modalPopupSearchCompany" style ="display:none">
<table width = "100%" cellpadding = "0" cellspacing = "0">
<tr>
<td align = "right">
<strong><asp:LinkButton ID="lnkCancel" runat="server" Text = "[X]"></asp:LinkButton></strong>
</td>
</tr>
<tr>
<td>
<uc2:SearchCompanies ID="SearchCompanies1" runat="server" />
</td>
</tr>
</table>
</asp:Panel>
My events are definitely being fired and handled. I have an update panel and update progress on the web user control.
Any ideas?

It turns out that after removing my UpdatePanel, it worked. I tried setting the UpdateMode to both "Always" and "Conditional" but to no avail.
This isn't a solution, but it's a workaround for anyone who's got the same problem until I figure out what I'm doing wrong. Let me know if anyone figures it out please.

Related

ImageButton OnClick event not working in asp.net

I have an Imageutton in asp.net. I want when I clicked on it, an event fire (that code of this event is written in c#).
asp code is:
<div align="right">
<table dir="rtl">
<tr>
<td>
<asp:ImageButton ID="imgScoreStar1" runat="server" ImageUrl="~/Images/ScoreStars/Star0_28px.png" OnClick="Star_Onclick" />
</td>
</tr>
</table>
</div>
and behind code is:
protected void Star_Onclick(object sender, ImageClickEventArgs e)
{
//do something
}
but ever fire click event (I tested it by breakpoint).
Another question: When I click on ImageButton, postback happens. how can I avoid this?
Thank.
Few things to check:
1. Check CausesValidation property. Test with making it false.
2. Use Page.RegisterRequiresRaiseEvent(ImageButton)
How to avoid postback.
Add ScriptManager. There must be only one ScriptManager in one page.
(If you are using Master page, add to master page and no need to add anymore in nested content page)
Add UpdatePanel and add your desired content including imagebutton to ContentTemplate of UpdatePanel.
in aspx
<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<div align="right">
<table dir="rtl">
<tr>
<td>
<asp:ImageButton ID="imgScoreStar1" runat="server" ImageUrl="~/Images/ScoreStars/Star0_28px.png" OnClick="Star_Onclick" />
</td>
</tr>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
in cs
protected void Star_Onclick(object sender, ImageClickEventArgs e)
{
//do something
}
i checked its working you need to check property of that button try to add another button and change ID of button that will Ignore the property that are stopping click event
You can surround your Page load sequence like this...
if(!IsPostBack){
//PageLoad code
}
Then write code under the button click event.
The button click event should work
It worked for me!

How to avoid post-back of Check Box inside repeater

I am newbie for asp.net,
I have a repeater which contains check box,whenever I check the check box I am firing an event on checkchanged,but the page postbacks.
I have an update panel for the entire content in my page,but still postback occurs.Is there anyway to avoid postback.
(Ps:To avoid Postback,I am meaning to avoid the flicker that occurs)
Thanks
<asp:Repeater ID="rptrDepartment" runat="server" OnItemCommand="rptrDepartment_ItemCommand"
OnItemDataBound="rptrdepartment_databound">
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID ="chkRow" runat="server" OnCheckedChanged="ChkRow_ChkChanged" AutoPostback="true" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
And in my .cs page,
protected void ChkRow_ChkChanged(object sender, EventArgs e)
{
//some method
}
Just keep your repeater inside update panel rather than entire page, if you want some other controls also need partial postback then you can go for multiple update panels.

ASP.NET ListView control not firing OnItemCommand Event

I know that this sounds like a number of other posts, all of which I have read but have not addressed my issue.
Here's the Scenario ...
I have BOTH a Repeater and a ListView bound to the same Data Source. Each control contains an ASP:LinkButton which, when clicked, should fire the OnItemCommand event. Although they are wired to the EXACT same data at the EXACT same places in the page life cycle and View State is enabled for the page and each individual control the Repeater appears to fire the event and the ListView does not.
I know that the event will not fire if the data is not bound BEFORE the assignment of the event handler. I am relying on ViewState to repopulate the controls when the page is posted back to. Looking at each control in debug mode while stepping through a request I can see that the Repeater DOES indeed appear to repopulated with the ViewState data but the ListView does not.
As these are both generated, populated, bound, and handled almost IDENTICALLY I am at a complete loss why this may be happening. I have also noticed a similar issue with the GridView control (it does NOT fire the event). I assume that these are related somehow.
The only thing that I can think of that the GridView and ListView have in common that the Repeater does not is the built-in paging capability. Whether implemented or not is there something with the paging that affects the loading of the ViewState?
OrderControl.ASCX is a control which exposes the ListView and Repeater as properties (OrderListLV & OrderListRPT) to the host page/application.
<asp:ListView runat="server" id="lvOrderList" OnItemDataBound="lstOrderList_OnItemDataBound" EnableViewState="true" >
<LayoutTemplate>
<table class="tblGrid">
<tr runat="server" id="itemPlaceholder" />
</table>
<ASP:DataPager runat="server" ID="dataPager1" PageSize="3">
<Fields>
<ASP:NextPreviousPagerField
ButtonType="Button"
ShowFirstPageButton="true"
ShowLastPageButton="true"
ShowNextPageButton="true"
ShowPreviousPageButton="true" />
</Fields>
</ASP:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr class="row-">
<td align="center"><ASP:LinkButton runat="server" id="lnkOrderId1" /></td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:Repeater runat="server" id="rptOrderList" OnItemDataBound="rptOrderList_ItemDataBound">
<HeaderTemplate>
<table class="tblGrid">
</HeaderTemplate>
<ItemTemplate>
<tr">
<td align="center"><ASP:LinkButton runat="server" id="lnkOrderId" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
OrderControl.ASCX.CS is where the controls are bound to the data source in the Pre-Render stage, well after ViewState has had the opportunity to reconstitute itself.
protected override void OnPreRender(EventArgs e)
{
this.lstOrderList.DataSource = this.OrderHeaders.OrderByDescending(x => x.OrderDate).ToList();
this.lstOrderList.DataBind();
this.rptOrderList.DataSource = this.OrderHeaders.OrderByDescending(x => x.OrderDate).ToList();
this.rptOrderList.DataBind();
}
Host.ASPX.CS is the page which consumes the control. It attaches the event handlers directly the controls in it's OnLoad handler.
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.OrderControl.OrderListRPT.ItemCommand += new RepeaterCommandEventHandler(OrderList2_ItemCommand);
this.OrderControl.OrderListLV.ItemCommand += new EventHandler<ListViewCommandEventArgs>(OrderList_ItemCommand);
}
After all is said and done when I click on the LinkButton in each control the ItemCommand Handler for the Repeater fires and executes correctly but the process doesn't even enter the handler for the ListView handler.
I am crazy confused on this issue. I am hoping that someone might have some thoughts on this. Something I can try at least?
Thanks,
Gary
you should set the CommandName property of each LinkButton in the ListView, eventually also the CommandArgument.
<td align="center">
<asp:LinkButton runat="server" id="lnkOrderId1" CommandName="yourCommandName" CommandArgument="yourCommandArgument" />
</td>
also check asked questions here in SO before posting a new question ;-)
listview OnItemCommand dosen't fire up

How do I acquire the value of a ASP label if it is nested? (client-side)

So there is a ASP checkboxlist that gets rendered to the table and in the code behind the checkboxes get bound with a label from a code table. I need to display a text box only if a certain checkbox is checked off.
<table id="chkSomeCheckbox">
<tbody>
<tr>
<td>
<input type="checkbox" id="chkSomeCheckbox_0">
<label for="chkSomeCheckbox_0">Acme</label>
</td>
<td>
<input type="checkbox" id="chkSomeCheckbox_1">
<label for="chkSomeCheckbox_1">Other</label>
</td>
</tr>
</tr>
</tbody>
</table>
The checkboxlist renders a table which the element retains the ID. I have tried to just get the value of the label when a checkbox is checked and then go from there to create my logic - but I can't seem to be able to grab the value of the label. I have tried the following different ways:
$("#<%chkSomeCheckbox.ClientID> input").next("label").val();
or
$("#<%chkSomeCheckbox.ClientID> input").next().val();
I am using the input selector because when I get this first part working, I will need to do some logic on it.
Any thoughts?
This is tagged as ASP.NET rather than ASP so I'm curious as to why you are not using ASP.NET controls. Also you have an extra </tr> tag in there.
You could do this:
<table id="chkSomeCheckbox">
<tr>
<td>
<asp:CheckBox ID="chkSomeCheckbox_0" runat="server"
Text="Acme" Checked="true" />
</td>
<td>
<asp:CheckBox ID="chkSomeCheckbox_1" runat="server"
Text="Other" Checked="false" />
</td>
</tr>
</table>
In the Page_Load event of the code behind you could then check the Checked property of the CheckBox and then decide to display the area that you held the Textbox in or just change the Visible property of the TextBox itself.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (chkSomeCheckbox_0.Checked)
{
txtSomeTextbox.Visible = true;
}
}
}
Tried this?
$("#<%= chkSomeCheckbox.ClientID %> input").next("label").val();
In your code sample the ASP.NET inline statement is not closed (and also the "=" sign is missing).
Note: This will only work in the aspx/ascx file, not in a external JavaScript file.
If you can grab a handle on the label that you want, you can probably call the .text() method to get the text of the label. As such, I don't think that labels have a "value"
For example, if you wanted to append a textbox after the label based on which one was checked you could do this:
$("#chkSomeCheckbox > input").change(function(){
$label = $(this).next();
if($(this).attr("selected") == true)
{
//get label text
foo = $label.text();
//append textbox after the label, etc
$label.append(textbox);
}
});A

Custom edit/delete button Gridview

I am using C#.net
I want to add custom edit/delete buttons to my GridView1 (one edit/delete button per row).
However I want the buttons to access another view (editView/deleteView within the same form), rather than edit ‘inline’ etc.
The edit button seems to be working fine. Here’s how I created it manually:
Right clicked on GridView1
Clicked on ‘Add New Column’
Field Type: ButtonField
Header Text: Edit
Button Type: Button
Command Name: Edit
Text: Edit
Within the ‘Events’ section (located under properties) for GridView1, I double clicked on the RowEditing, this then created a Event I could access within the code behind.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
// Access _viewAdd
_multiView1.ActiveViewIndex = 1;
}
The delete button should access the deleteView (confirmation page) rather than just automatically deleting a row. I want to create a custom method that is triggered when the user selects the delete button.
I ended up using a repeater and amending both a edit/delete button onto the end of each row. These button not only held the OnClick_Event information but also the ID associated with that row.
<asp:Repeater ID="Repeater" runat="server" DataSourceID="*****">
<HeaderTemplate>
<table cellpadding="3" cellspacing="3">
<tr>
<th style="text-align:left">Name</th>
<th> </th>
<th> </th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td style="text-align:left"><%#Eval("forename")%> <%#Eval("surname")%></td>
<td style="text-align:left"><asp:Button ID="edit" OnCommand="edit_Click" CommandArgument='<%#Eval("id")%>' runat="server" Text="Edit" CssClass="standardButton" /></td>
<td style="text-align:left"><asp:Button ID="delete" OnCommand="delete_Click" CommandArgument='<%#Eval("id")%>' runat="server" Text="Delete" CssClass="standardButton" /></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
I hope that helps other people.
There is a RowDeleting event you can handle as well. Both event args have a Cancel property you can set to true to prevent the data from being modified.

Categories