Showing posts with label fires. Show all posts
Showing posts with label fires. Show all posts

Wednesday, March 28, 2012

LinkButton OnCommand UpdatePanel

Somehow, the OnCommand method of a LinkButton never fires in the following code. I believe I'm running the latest of Ajax (1.0) and the Microsoft.Web.Preview.dll is there in my Bin directory. Any clue? I'm pretty sure I got this working when I didn't have UpdatePanel.

.ASPX code:

1<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">2 <asp:ScriptManager ID="ScriptManager1" runat="server">3 </asp:ScriptManager>4 <asp:UpdatePanel ID="up1" runat="server">5 <ContentTemplate>6 <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" Font-Size="X-Small"7 Width="100%" ShowHeader="false" BorderStyle="none">8 <RowStyle BackColor="#ffffff" />9 <Columns>10 <asp:ImageField DataImageUrlField="status" DataImageUrlFormatString="~/Images/{0}.png"11 NullDisplayText=" - " ItemStyle-HorizontalAlign="center" DataAlternateTextField="description" />12 <asp:TemplateField>13 <ItemTemplate>14 <asp:Literal runat="server" ID="appName" EnableViewState="false" Text='<%# Eval("app_name") + " (" + Eval("application_id") + ")"%>'></asp:Literal>15 </ItemTemplate>16 </asp:TemplateField>17 <asp:TemplateField ItemStyle-HorizontalAlign="center">18 <ItemTemplate>19 <asp:LinkButton runat="server" ID="linkPromote" Text='<%# this.GetLink(Convert.ToInt32(Eval("status")))%>'20 Visible='<%# this.ShowLink(Convert.ToInt32(Eval("status")))%>' OnClientClick="Ask();"21 CommandArgument='<%# Eval("application_id") + "|" + Eval("run_date") + "|" + Eval("status")%>'22 CommandName='<%# this.GetLink(Convert.ToInt32(Eval("status")))%>' OnCommand="ChangeStatus" />23 </ItemTemplate>24 </asp:TemplateField>25 <asp:TemplateField ItemStyle-HorizontalAlign="center">26 <ItemTemplate>27 <asp:HyperLink ID="details" runat="server" NavigateUrl='<%# "javascript:MasterList(" + this.GetRowData(Container) + ");"%>'28 ImageUrl="~/Images/more.gif" ToolTip="Details" Visible='<%# this.ShowDetails(Convert.ToInt32(Eval("status")))%>' />29 </ItemTemplate>30 </asp:TemplateField>31 </Columns>32 </asp:GridView>33 </ContentTemplate>34 </asp:UpdatePanel>35 <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetListOfRunningAppsNotCompleted"36 TypeName="AnalyticsBizOb.StatusMonitorImp"></asp:ObjectDataSource>3738 <script type="text/javascript">39 function Ask()40 {41 return confirm('Are you sure you want to change status for this App?');42 }43 </script>4445</asp:Content>

Code-behind:

1protected void Page_Load(object sender, EventArgs e)2 {3if (!IsPostBack)4 {5 PopulateRunningAppsGrid();6 }7 }8void PopulateRunningAppsGrid()9 {10 ObjectDataSource2.SelectParameters.Clear();11 ObjectDataSource2.SelectParameters.Add("runDate","20070419");12 ObjectDataSource2.DataBind();13 GridView2.DataSourceID ="ObjectDataSource2";14 GridView2.DataBind();1516 }17protected void ChangeStatus(object sender, CommandEventArgs e)18 {19if (e.CommandName =="Restart" || e.CommandName =="Promote")20 {21 StatusMonitorImp smi =new StatusMonitorImp();22string[] args = e.CommandArgument.ToString().Split('|');23int statusTo = e.CommandName =="Restart" ? 19 : 20;2425 ScriptManager.RegisterStartupScript(up1, up1.GetType(),"success","alert('Successfully changed the status of app " + args[0] +" from " + args[2] +" to " + statusTo.ToString() +" for date " + args[1] +".');",true);2627 }28 GridView2.DataBind();29 }30public string GetRowData(IDataItemContainer iic)31 {32if (iic !=null)33 {34return DataBinder.Eval(iic.DataItem,"run_date").ToString() +", "35 + DataBinder.Eval(iic.DataItem,"application_id").ToString() +", '"36 + DataBinder.Eval(iic.DataItem,"app_name").ToString() +"'";37 }38else39 return"";40 }41protected bool ShowLink(int status)42 {43if (status == 19 || status == 50 || status == 500)44return true;45else46 return false;47 }4849protected bool ShowDetails(int status)50 {51if (status > 0 && status != 999)52return true;53else54 return false;55 }56protected string GetLink(int status)57 {58string ret ="";59if (status == 500 || status == 50)60 ret ="Restart";61if (status == 19)62 ret ="Promote";63return ret;64 }

Found the reason why it was not working. In my MasterPage, I've turn off the ViewState (EnableViewState = "false"), that's why the code above (OnCommand="ChangeStatus") was not firing up.

I don't know whether it is by design but I think it's bug in ASP.Net and something is not right in the core.

ListBox - SelectedIndexChanged Problem

I have a data bound ListBox. When I click on an item it fires the SelectedIndexChanged event like it should, however, NONE of the items are marked as 'selected' in the event method. Any ideas?

Doh! Found the solution! I was rebinding the data to the listbox on postback and apparently this is what was causing the problems. I wrapped the data binding code in an IF statement that executes if IsPostBack is false.

This might be good to know for others out there.