Custom edit/delete button Gridview - c#

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.

Related

How to create a RadioButtonList where each radio button is in a separate table row?

I'm creating an MCQ Question and answer Interface where I need to place my each radio option in separete row next to the answer, so that user can select the answer, I know to use radiobutton list in a single row in normal way,
<asp:RadioButtonList ID="opt_questiontype" runat="server" RepeatLayout="Flow" OnClick="call()">
<asp:ListItem Value="MCQ" Selected="True">MCQ</asp:ListItem>
<asp:ListItem Value="Single">Single Answer</asp:ListItem>
</asp:RadioButtonList>
but here i need to place them in row by row in an html table as follows,
<table id="mcqtable">
<tr style="border-bottom-style:solid; border-bottom-width:1px; border-bottom-color:#CCCCCC;">
<th class="captions2">Option</th><th class="captions2">Answer Text</th><th class="captions2">Is Correct</th>
</tr>
<tr>
<td class="captions1">Answer Option 1</td><td><asp:TextBox ID="txt_answeropt1" runat="server" TextMode="SingleLine" Width="600px"></asp:TextBox></td><td><asp:RadioButton ID="opt_answer1" runat="server" GroupName="grp_answers" Checked="true" /></td>
</tr>
<tr>
<td class="captions1">Answer Option 2</td><td><asp:TextBox ID="txt_answeropt2" runat="server" TextMode="SingleLine" Width="600px"></asp:TextBox></td><td><asp:RadioButton ID="opt_answer2" runat="server" GroupName="grp_answers" /></td>
</tr>
<tr>
<td class="captions1">Answer Option 3</td><td><asp:TextBox ID="txt_answeropt3" runat="server" TextMode="SingleLine" Width="600px"></asp:TextBox></td><td><asp:RadioButton ID="opt_answer3" runat="server" GroupName="grp_answers" /></td>
</tr>
<tr>
<td class="captions1">Answer Option 4</td><td><asp:TextBox ID="txt_answeropt4" runat="server" TextMode="SingleLine" Width="600px"></asp:TextBox></td><td><asp:RadioButton ID="opt_answer4" runat="server" GroupName="grp_answers" /></td>
</tr>
</table>
Here I have used normal radiobuttons, but there is a conflict when getting the selected value, So please any one could suggest a way to use asp:RadioButtonList to do this task?
here I have used normal radiobuttons, but there is a conflict when
getting the selected value
What is conflict? why you want to use RAdioButtonList here? Your existing code does not show me any conflict.
Either use repeater or you can use css to create single row for each radio button.
In repeater you can control the HTML as per your requirement
You should set RepeatDirection="Vertical" in order to show the options row-wise.
Please refer my answer #CodeProject - Change ASP radiobuttonlist repeatdirection property with Javascript or JQuery for a more detailed information on how we can toggle between Vertical and Horizontal alignments.
Demo - [Demo] Change Repeat Direction of asp RadioButtonList
EDIT
Do like below...
<asp:RadioButtonList ID="opt_questiontype" runat="server" RepeatLayout="Flow" RepeatDirection="Vertical" OnClick="call()">

Setting my TextBox Text value isn't doing anything

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.

Sorting Listview

I have A listview which I want to sort.
My problem is that when I click the sorting column the Onsorting event only fires when I Bind the data on pageload again.
This means that on every pageload I will first have to bind the data, then I can catch the OnBinding event and after that I can Rebind the data again.
Is there a better way to do this. Basically what I want is to bind the data only in the onsorting event
<asp:ListView ID="TempList" runat="server" OnSorting="TempList_sorting">
<LayoutTemplate>
<table >
<tr>
<th >
<asp:LinkButton runat="server" ID="btnSortVoorletters2" CommandName="Sort" Text="Voorletters"
CommandArgument="Voorletters" OnClick="btnSortVoorletters_Click" />
</th>
</tr>
<tr runat="server" id="itemPlaceholder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="EmpIDLabel" runat="server" Text='<%# Eval("Naam") %>'/>
</td>
</tr>
</ItemTemplate>
<EmptyDataTemplate>
<p>Empty text that will be displayed.</p>
</EmptyDataTemplate>
It lloks that you're always binding in PageLoad so that the previously supplied order is lost. Bind only if IsPostback is false. (You need to have the ListView EnableViewState set to true, which is the default value).
if(!IsPostback)
{
// do the binding
}
When the user clicks the 'Sort' button, the event will be fired and your event will sort and bind the data accordingly.
If the page is reloaded, and you don't rebind it in PageLoad this will be enough.
However, if for some reason you really need to rebind in PageLoad, what you have to do is to store the sorting in ViewState, a HiddenField or Session so that you can get the value from there to sort the data before binding it in PageLoad.
You should take the code that sorts and binds to a new method, and call it from both the Sort and the PageLoad events.

Need help with repeater

This is my repeater:
<asp:Repeater ID="myRepeater" OnItemCommand="myRepeater_ItemCommand" runat="server" OnItemDataBound="myRepeater_OnItemDataBound">
<HeaderTemplate>
<table width="99%" border="0" cellpadding="0" cellspacing="0">
<tr class="lgrey">
<td>Default</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="lnk1" Text="Make Default" CommandName="SetDefault" runat="server" Visible="True" CommandArgument='<%#Eval("UserID") %>' CausesValidation="false"></asp:LinkButton>
<asp:Label ID="label1" Text="Yes" runat="server" Visible="False"></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
What I want is that when user clicks on any of
the "lnk1" link button in the list that repeater renders,
the link should be replaced with the label "label1"..
i.e. when the user clicks on "Make Default" link, it should be replaced with "Yes" label
Calling this method obj.SetDefaultAddress(); is setting the default address in the DB alright..
problem is with the display of the label1 and lnk1 when the repeater renders...
what is happening is that BOTH "Make Default" LinkButton and the "YES" label are getting displayed
under the "Default" column of the table inside my repeater.
I want some code that will check the "IsDefault" value in my DB and display "Make Default " link button
and "YES" label accordingly... i.e. if IsDefault's value in the DB is TRUE then "YES" should be displayed in the repeater
otherwise "Make Default"
Are you sure your piece of code in code behind under ItemCommand is executing?
I only changed the CommandName from SetDefault to SetDefaultAddress in aspx file to match with the one in code behind, it worked.
Where to start...
I think what's causing your problem is that the SelectedItem and the DefaultAddress are not mapped to each other, so when you click the button you're getting the selected index set and the OnItemDatabound event is showing/hiding what you want, but when the grid is initialized from the database, the SelectedItem is not being set.
I don't know what your datasource is, and there's obviously more code to this than what you've posted, but if you can look at the e.Item.DataItem in the myRepeater_ItemDataBound handler, you can set the current item as selected when the address is the default (e.Item.ItemType... or use your "selectedIndex" counter)
I will probably do it from markup itself - this is assuming that you have "IsDefault" column/property of bit/boolean type in your data-source indicating the address is default. So use following markup:
...
<tr>
<td>
<asp:LinkButton ID="lnk1" Text="Make Default" CommandName="SetDefault" runat="server" Visible='<%# !Eval("IsDefault") %>' CommandArgument='<%#Eval("UserID") %>' CausesValidation="false"></asp:LinkButton>
<asp:Label ID="label1" Text="Yes" runat="server" Visible='<%# !Eval("IsDefault") %>'></asp:Label>
</td>
</tr>
...
You need to control visibility based on property in your data source (either using markup or ItemDataBound event). Also when SetDefault link is clicked, you must either re-bind the repeater new state or toggle visibility explicitly (as your current code is doing).
EDIT:
If data binding expression are not working then you have to do it in ItemDataBound event. I see that you have already tried that but there is one mistake - bllUsers obj=new bllUsers(); will always have IsDefault as false - you need to use data item. For example,
protected void myRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
bllUsers obj = e.Item.DataItem as bllUsers;
((Label)e.Item.FindControl("ldefault")).Visible = obj.isDefault;
((Button)e.Item.FindControl("btnMakeDefault")).Visible = ! obj.isDefault;
}
}

How can I use the ScrollTo jQuery plugin to scroll to particular row in a repeater in ASP.NET?

I have a simple repeater that looks like:
<asp:Repeater runat="server" ID="rptOptions" OnItemDataBound="rptOptions_ItemDataBound">
<HeaderTemplate>
<thead>
<tr>
<td class="GridHeader">Account</td>
<td class="GridHeader">Margin</td>
<td class="GridHeader">Symbol</td>
<td class="GridHeader">Usymbol</td>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tbody>
<tr runat="server" ID="trOption">
<td class="GridRow"><asp:Label runat="server" ID="lblOptionAccount"></asp:Label></td>
<td class="GridRow"><asp:Label runat="server" ID="lblOptionMargin"></asp:Label></td>
<td class="GridRow"><asp:Label runat="server" ID="lblOptionSymbol"></asp:Label></td>
<td class="GridRow"><asp:Label runat="server" ID="lblOptionUsymbol"></asp:Label></td>
</tr>
</tbody>
</ItemTemplate>
</asp:Repeater>
Now, in my code-behind I have an event which is fired that is supposed to add/insert a row into the database. After this happens, I re-grab the new list of options from the database and re-bind them to the repeater. This takes place inside an update panel so the list refreshes right away for the user.
protected void lbtnAddOptionSave_Click(object sender, EventArgs e)
{
SelectedOption = new Option()
{
Account = txtAddOptionAccountNumber.Text,
Margin = chkAddOptionMargin.Checked,
Symbol = txtAddOptionSymbol.Text,
Usymbol = txtAddOptionUsymbol.Text,
};
Presenter.OnAddOption(); // Insert new option into database
RefreshOptions(); // Re-get list of options, bind them to repeater
}
Now, what I would ~love~ to do, is use the jQuery ScrollTo plugin to scroll straight to the newly added row.
What would be the best way to call the ScrollTo() method in the jQuery plugin so I scroll to that particular row that was just added? Is there anyway I can mark my rows in my ItemTemplate so I can easily select an element to scroll to via jQuery?
Ideally, right after RefreshOptions() I would like to execute the ScrollTo function to scroll down to the new row.
If you know the client side Id of the row (which you can get), its relatively painless to simply call
$(document).scrollTo("#<row-id-here>", 800);
When you add the object to the database (or just after that), grab the id of the newly inserted object. Modify the repeater to include a Label with Visible="false" so that it doesn't get rendered to the client. Hook into the ItemDataBound event and check each label against the id you've grabbed. When you find the matching row, you can get the id of the row and then you'll be able to use for the scrolling parameter.
Note: Other data-bound controls have a DataKey property which could be used for the id of the object and make this a bit simpler. Not sure if you're tied to the Repeater at this point, but a GridView or ListView could be worth looking into.

Categories