In ASP.NET, how do I change the visibility of a button in the same column of a gridview itemtemplate when the other button is clicked? - asp.net

I have the following gridview:
<asp:GridView ID="gridOpenMECs" runat="server">
<Columns>
<ItemTemplate>
<asp:ImageButton ID="btnShow" runat="server" ImageUrl="xxx.png"
OnClick="btnShow_OnClick" />
<asp:ImageButton ID="btnHidden" runat="server"
ImageUrl="yyy.png" Visible="false" />
</ItemTemplate>
</Columns>
</asp:GridView>
When button1's onclick serverside event is fired I want to obtain a handle on button2 so that I may change its Visible attribute to True.
Protected Sub btnShow_OnClick(ByVal sender As Object, ByVal e As ImageClickEventArgs)
Dim btn as ImageButton = CTYPE(sender, ImageButton) 'get the sending button handle
'' what next to make btnHidden visible?
End Sub
How can I accomplish this? Thank you.

Sorry, C# speak ...
GridViewRow whichrow = (GridViewRow)btn.NamingContainer;
ImageButton btnHidden = (ImageButton)whichrow.FindControl("btnHidden")

Related

Ajax CalendarExtender not displaying 2nd row on

I have a Ajax CalendarExtender, ID'd to a textbox and data sourced to more than one record.
If the textbox's are all blank, no assigned value. The textbox's will popup the calendar with click.
If the textbox's contain a value, the first textbox will pop a blank calendar and the rest will pop nothing.
I have stand-alone textbox's with the ajax calendar and they work fine. Here is what I have with assigned value.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "false">
<Columns>
<asp:TemplateField HeaderText="Maturity Date">
<ItemTemplate >
<asp:TextBox ID="txtMatureDate" text="4" runat ="server"></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="maturitydate" TargetControlID="txtMatureDate" Format="MMMM,yyyy" runat ="server" DefaultView="Months" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Here is datasource.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim SqlDataSource3 As New SqlDataSource()
SqlDataSource3.ID = "SqlDataSource3"
Me.Page.Controls.Add(SqlDataSource3)
SqlDataSource3.SelectCommand = "select maturitydate from PFW_LPMCalc_test where maturitydate >= '02/01/2017' and maturitydate <= '07/31/2017' order by maturitydate desc"
SqlDataSource3.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("jdwholegoods").ConnectionString
GridView1.DataSource = SqlDataSource3,
GridView1.DataBind()
End Sub
I have scriptmanager in place and
Ok.. so I uninstalled the Ajax Toolkit and reinstalled. It had a newer version though I don't think that did it. My guess is something was on top of current version and it needed to be back on top.

Repeater FooterTemplate Button Click Event Not being handled

When debugging, clicking on btnSignup does not hit the breakpoint on the first line of my btnSignup_Click event.
Just to see if it'd help I removed the datasourceID from the repater and created a Sub routine to databind the repeater on page_load if-not-isPostback. That didn't help.
What do I need to do to get my btnSignup to fire my btnSignup_Click event?
I have this on my ASCX
<asp:Repeater ID="rptParticipants" runat="server" DataSourceID="sdsParticipants">
<ItemTemplate>
<p class="participant"><span class="participant-number"><%# Container.ItemIndex + 1 %>.</span> <span class="participant-name"><%# Container.DataItem("name")%></span></p>
</ItemTemplate>
<FooterTemplate>
<br />
<asp:Literal ID="litBlanks" runat="server"></asp:Literal>
<div class="text-center">
<asp:Button ID="btnSignup" runat="server" Text="Sign Me Up" CssClass="btnSignup" OnClick="btnSignup_Click" />
<asp:Button ID="btnRemove" runat="server" Text="Remove Me" CssClass="btnRemove" OnClick="btnRemove_Click" />
</div>
</FooterTemplate>
</asp:Repeater>
I have this on my ASCX.vb
Public Sub btnSignup_Click(sender As Object, e As System.EventArgs)
CheckAvailability() 'Breakpoint on this line
'more code here
End Sub
Try using the ItemCommand Event on the Repeater to handle your button clicks. Replace the OnClick Attribute with the CommandName as shown below:
<asp:Button ID="btnSignup" runat="server" Text="Sign Me Up" CssClass="btnSignup" CommandName="SignUp" />
Next, create an event handler for your Repeater's ItemCommand Event:
Protected Sub rptParticipants_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rptParticipants.ItemCommand
If e.CommandName = "SignUp" Then
btnSignup_Click(Me, New System.EventArgs)
End If
End Sub

Update Hyperlink In Gridview not working

Gridview is not working when user click on the link. User is supposed to be directed to another page when clicked on the link and on the second page, there is a text box that will allow user to update.
this is my code for grid view:
<asp:BoundField DataField = "status" HeaderText = "Status" HtmlEncode = "true" ItemStyle-Width="150px" >
<ItemStyle Width="50px" />
</asp:BoundField>
<asp:ButtonField ButtonType="Link" Text="Click" CommandName="Select" HeaderText="Details" />
<asp:ButtonField HeaderText="Update" Text="update?" CommandName="GridView1_RowUpdated" />
code behind:
Protected Sub GridView1_RowUpdated(sender As Object, e As GridViewUpdatedEventArgs)
Dim cr_number As String = GridView1.SelectedRow.Cells(0).Text
Response.Redirect("upddetail.aspx?id=" + cr_number)
End Sub
since you are set the CommandName as GridView1_RowUpdated you can use GridView1_RowCommand event as below
Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
If e.CommandName = "GridView1_RowUpdated" Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim cr_number As String = GridView1.Rows(index).Cells(0).Text
Response.Redirect("upddetail.aspx?id=" + cr_number)
End If
End Sub
in your aspx page set onrowcommand as below
<asp:gridview id="GridView" onrowcommand="GridView1_RowCommand"
instead of all above you can use hyperlinkfield
<asp:hyperlinkfield text="Update?"
datanavigateurlfields="Id"
datanavigateurlformatstring="~\upddetail.aspx?id={0}"
headertext="Update"
target="_blank" />
You are using ButtonField not an actual hyperlink. When user clicks on this there will be a postback to your original page and it will not navigate to another page. Try using HyperLink.

Formview.ChangeMode is not working

This is my webpage:
<asp:FormView
ID = "frmView1"
DefaultMode = "Insert"
runat = "server"
>
<ItemTemplate>
Item Template
</ItemTemplate>
<InsertItemTemplate>
Insert Item Template
</InsertItemTemplate>
</asp:FormView>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
and this is the code-behind:
Private Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
frmView1.ChangeMode(FormViewMode.ReadOnly)
End Sub
When I'm clicking the btnSubmit the formview is not showing anything! Is this the right way to change mode in code behind?
Moreover, in code behind instead of ChangeMode if write this:
frmView1.DefaultMode = FormViewMode.ReadOnly
forview shows the content of InsertItemTemplate. Could someone please explain what's going on here?
FormView won't show unless you bind, something like this:
using(SqlDataAdapter adapter = new SqlDataAdapter(sql,connection))
{
DataTable table = new DataTable();
adapter.Fill(table);
FormView1.DataSource = table;
FormView1.DataBind();
}
And just add EmptyDataTemplate tag to show empty message if your datasource don't return any value.
<EmptyDataTemplate>
There is nothing to see here.
</EmptyDataTemplate>
follow this tutorial and more about this on MSDN
In your code
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
must change to
<asp:Button ID="btnSubmit" Text="Submit" runat="server" command="ChangeModeToReadOnly" OnCommand="Button_Command" />
Instead of implementing OnClick event implement this
Protected Sub Button_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
Select Case e.CommandName
Case "ChangeModeToReadOnly"
frmView1.ChangeMode(FormViewMode.ReadOnly)
End Select
End Sub
You can try with adding the following DataBound function
protected void myFormView_DataBound(object sender, EventArgs e)
{
frmView1.ChangeMode(FormViewMode.ReadOnly);
}
The syntax is in C#

asp.net/VB: user control in repeater - catch click event of button

i have a User Control.
In this user control I have a Repeater.
In this repeater I have again a User Control.
If I try to catch the click event of an ImageButton in this user control, I'm gettin nothing because the event has not been fired.
Is there a way to catch this event?
some code:
first user control:
<asp:Repeater ID="Rpt" runat="server">
<ItemTemplate>
<uc1:myUserControl id="myUserControl1" runat="server" />
</ItemTemplate>
</asp:Repeater>
in myUserControl
<asp:LinkButton ID="myUserControlBtn" runat="server" OnClientClick="thisEventIsFiring();" OnClick="btn_Click" Text="btn" />
This event isn't firing:
Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
' I am not firing
End Sub
Also the following does not work because it returns nothing:
If Request(btn.UniqueID) IsNot Nothing
'check if Button was clicked
End If
It should be
<asp:LinkButton ID="myUserControlBtn" runat="server" OnClick="btn_Click" Text="btn" />

Resources