inside of while loop, i've used
<asp:Button ID="<%=objReader.Item(0)%>" OnClick="btn_Click" runat="server" CssClass="submit_button" Text="Delete" />
Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs)
'You have clicked button
End Sub
to creating the button dynamically. Now, on click of particular button, it should show the information which are associated with the clicked button. Need Help !!
Solution
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="Jobs">
<ItemTemplate>
<asp:Button ID="btnDemo" CommandArgument='<%#Eval("Login_id")%>' OnCommand="btnDemo_Click" runat="server" Text="Button" /></ItemTemplate>
</asp:Repeater>
Sub btnDemo_Click(ByVal sender As Object, ByVal e As CommandEventArgs)
MsgBox(e.CommandArgument)
End Sub
Thanks to every one!!
It's better to implement this in a databound control, like the repeater or listview.
You could then use the CommandArgument of a button to add some arguments which are distinct for each button.
You can then handle this in the Click procedure to handle the right action at the right CommandArgument.
see:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx
Example:
<asp:Repeater runat="server" ID="rptList">
<ItemTemplate>
<asp:Button runat="server" ID="btnDemo" CommandArgument='<%#Eval("Id") %>' Text="Click me" OnCommand="btnDemo_Click" />
</ItemTemplate>
</asp:Repeater>
Related
I have an asp.net datagrid which looks like this. I don't seem to be able to get the linkbuttons to fire.
<asp:DataGrid ID="dgrdItem" runat="server" OnItemDataBound="dgrdItem_ItemDataBound" Width="100%" AutoGenerateColumns="False" OnSelectedIndexChanged="EditItem" >
<HeaderStyle CssClass="datagridheaderstyle"></HeaderStyle>
<Columns>
<asp:TemplateColumn ItemStyle-Width="240" HeaderText="Name">
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem, "Name")%></ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Notes" HeaderText="Comments" ItemStyle-Width="180" />
<asp:TemplateColumn HeaderText="Options">
<ItemTemplate>
<table>
<tr>
<span class="infobarbutton">
<td id="tdEdit" runat="server">
<asp:LinkButton ID="btnEdit" Text="Edit" CommandName="Select" Width="52" runat="server" />
</td>
</span>
</tr>
</table>
</asp:TemplateColumn HeaderText="Options"></asp:DataGrid>
So the above is a stripped down version of the datagrid. Im then trying to fire the below handler which is all wired up correctly I believe.
Protected Sub EditItem(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
'Do Some Work here...
End Sub
Any advice on where I am going wrong would be really helpful. Thank you.
You need to handle the ItemCommand event. Change OnSelectedIndexChanged="EditItem" to OnEditCommand="EditItem". Or you could just remove it from markup and add a handles clause like below:
Protected Sub EditItem(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgrdItem.ItemCommand
If e.CommandName = "Edit" ...
End Sub
I have an asp.net website in which I'm using an AJAX TabContainer. Within this container I have one TabPanel. Inside this panel I want to have a close button which would, upon clicking, close (visible=false) this panel.
Sample:
<cc1:TabContainer ID="tbcMain" runat="server" Width="100%" ActiveTabIndex="0" CssClass="Tab">
<cc1:TabPanel ID="tbp1" runat="server" Visible="false">
<HeaderTemplate>
<asp:Label ID="lbl_tbp1_Title" runat="server"></asp:Label>
<asp:ImageButton ID="imb_tbp1_Close" runat="server" ImageUrl="~/Images/cross.png" />
</HeaderTemplate>
<ContentTemplate>
some content
</ContentTemplate>
</cc1:TabPanel>
</cc1:TabContainer>
The button imb_tbp1_Close does not cause a postback. I belive it's something with the whole region of HeaderTamplate being clickable and thus my button stays behind (like a z-index of sorts).
How can I make my button receive the click and cause a postback?
Edit, the solution I'm using now:
Firstly I could use an actual button event in the code-behind but ImageButton control does not have the property UseSubmitBehavior to set it false. So I handle the event in my Page_Load.
<cc1:TabPanel ID="tbpCust_1" runat="server" Visible="false">
<HeaderTemplate>
<asp:Label ID="lbl_UC_CUSTO_1_Title" runat="server"></asp:Label>
<asp:ImageButton ID="imb_CUSTO_Close_1" runat="server" ImageUrl="~/Images/cross.png" OnClientClick="javascript:__doPostBack('imb_CUSTO_Close_1', '0')" />
</HeaderTemplate>
<ContentTemplate>
<ucCUSTO:UC_CUSTO ID="UC_CUSTO_1" runat="server" />
</ContentTemplate>
</cc1:TabPanel>
and in the code-behind:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim evtTarg As String = Request.Form("__EVENTTARGET")
Dim evtArg As String = Request.Form("__EVENTARGUMENT")
If evtTarg = "imb_CUSTO_Close_1" Then
UC_CUSTO_1.clear_fields()
tbcStranke.Tabs(0).Visible = False
ElseIf evtTarg = "imb_CUSTO_Close_2" Then
UC_CUSTO_2.clear_fields()
tbcStranke.Tabs(1).Visible = False
End If
End Sub
I am trying to put the UserName of the authenticated user on an Insert item template. I need the easiest explanation since I am very new. I really appreciate your help. This is what I have:
<InsertItemTemplate>
<asp:LoginName ID="LoginName2" runat="server" User.Identity.Name='<%# Bind("UserName") %>'/>
</InsertItemTemplate>
Thanks
Jose
Do this from the codebehind
<asp:DetailsView ID="DetailsView1" runat="server" OnDataBound="DetailsView1_DataBinding">
<InsertItemTemplate>
<asp:LoginName ID="LoginName2" runat="server" />
</InsertItemTemplate>
</asp:DetailsView>
Protected Sub DetailsView1_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.DataBinding
LoginName2.text =User.Identity.Name
End Sub
I am trying to implement an update panel on my web page. when I add this, everything works fine:
<script runat="server">
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Label2.Text = Date.Now.ToString
End Sub
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<fieldset>
<asp:Label ID="label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:Button ID="Button2" runat="server" Text="Button" />
</fieldset>
</ContentTemplate>
</asp:UpdatePanel>
<asp:TextBox ID="TextBox1" runat="server" Height="289px" TextMode="MultiLine"
Width="663px" ReadOnly="True"></asp:TextBox>
The problem comes when I try to do some stuff on the on_load event of the application. in the code behind, i try to do this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
If Not IsPostBack Then
....a function that produces a very long xml string
Me.TextBox1.Text ="<f"
End If
End Sub
This will cause the event to not occur. If i change the "<" from the string, all is well. why cant I have "<" in my strings? This is important because i wont allow me to put an xml string in the text box.
any help would be great!
Try using << rather than <
I have a gridview with a button, and when the button is clicked, it fires a rowcommand procedure and adds a new row to the database. Everything works fine until I add a databound drop down list to the gridview.
With a databound dropdown list, the page loads fine, but when I click the button the error shows as "Internet Explorer cannot display the webpage". here is my code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand"
DataSourceID="SqlDataSource1">
<Columns>
<asp:ButtonField CommandName="insertNew"
Text="Button" />
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnAdd" runat="server" CommandName="insertNew"
CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
Text="Add" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField></asp:TemplateField>
</Columns>
</asp:GridView>
And here is my code behind that runs when the button is pressed;
Protected Sub GridView1_RowCommand(ByVal sender As Object, _
ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
If (e.CommandName = "insertNew") Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
MsgBox(index)
End If
End Sub
I just had to put this into the system.web web.config file
<httpRuntime maxRequestLength="32768" />
The problem is in the MsgBox line. MsgBox(index) is not supported in web applications.
Please remove MsgBox(index), and the issue will be fixed, because that feature is only supported in Windows Applications.