ValidationGroup on Fileupload - c#

I have a FormView where there is a FileUpload with a RequiredFieldValidator and I need to have a validationGroup on the FileUpload or else my Button won't go it's PostBackUrl.
My code:
<asp:FormView ID="fwResultsInsert" runat="server" DataKeyNames="ResultsID"
DataSourceID="ObjResults" DefaultMode="Insert">
<InsertItemTemplate>
<asp:Label ID="lblImagess" runat="server" Text="Resultat Billede:"></asp:Label>
<asp:FileUpload ID="fuResultsImages" runat="server" />
<asp:RequiredFieldValidator ID="rfvImage" runat="server" ErrorMessage="*Her mangler noget!!" ControlToValidate="fuResultsImages"></asp:RequiredFieldValidator>
<asp:Label ID="lblOutput" runat="server" Text="" Visible="True"></asp:Label>
<asp:Label ID="Error" runat="server" Text="" Visible="True"></asp:Label>
<asp:ImageButton ID="imgPicture" runat="server" Visible="True" />
<br />
<asp:LinkButton ID="btnInsert" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insæt" onclick="btnInsert_Click" ForeColor="#000000" Font-Underline="True" />
<asp:LinkButton ID="btnCancel" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Fortryd" ForeColor="#000000" Font-Underline="True" />
</InsertItemTemplate>
</asp:FormView>
<asp:Button ID="btnHome" runat="server" Text="Forsiden" PostBackUrl="~/BackEnd/Default.aspx" />
I need to have some kind of validation on my FileUpload, so there have to be something in the fileupload-box when I clink insert, atm I have a RequiredFieldValidator, but there is a conflict with another Button I have outside the FormView, atm when I click the button nothing happens unless there is something in the FileUpload, and that's not the point.

Add CausesValidation="false" to the Button which you have outside the FormView.
Ex:
<asp:Button ID="btnHome" runat="server" Text="Forsiden" PostBackUrl="~/BackEnd/Default.aspx" CausesValidation="false" OnClick="btnHome_Click" />

Related

Button click not working inside gridview inside updatepanel

I have a div that has a gridview inside an updatepanel, that I use bootbox.js to show modal.
Inside the gridview is an itemtemplate with an asp.net button "Button1" which I need to click to edit the row after modalpopup but this doesnt fire
Please what am I missing
<div id="userForm" class="form-horizontal" style="display:none;width:100%;border:solid 0px;" >
<input ID="btnshowRun" type="button" Value="Show Leave" class="btn btn-warning " style="float:lefts;" OnClick ="showhide();"/>
<asp:UpdatePanel ID="UpdatePanel5" runat="server" style="display:none;">
<ContentTemplate>
<asp:GridView ID="GridView2" runat="server" AllowSorting="False"
AutoGenerateColumns="False" DataKeyNames="EntryID"
EmptyDataText="There are no data records to display."
ShowFooter="False" HorizontalAlign="Center" OnRowCommand="gvl_RowCommand"
ShowHeaderWhenEmpty="True" CssClass="table table-striped table-bordered table-hover table-responsive table-condenseds " Width="100%" GridLines="None">
<Columns>
<asp:TemplateField HeaderText="Total" SortExpression="Total">
<EditItemTemplate>
<asp:TextBox ID="tot" runat="server" Text='<%# Bind("Total") %>' class="form-control"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("Total") %>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="CommandButton" runat="server" CommandArgument='<%# Eval("EntryID") %>' UseSubmitBehaviour=true CausesValidation="False"
CommandName="Update" OnCommand="Updates_Command" Text="Update" class="btn btn-primary btn-sm contrl" Style="margin-right: 10px;"
/>
<asp:LinkButton ID="Button2" runat="server" CausesValidation="False" CommandName="Cancel" class="btn btn-warning btn-sm contrl" Style="margin-right: 10px;"
Text="Cancel" />
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="Button1" runat="server" CommandArgument='<%# Eval("EntryID") %>' class="form-control btn btn-default btn-blocks contrl btn-sms"
CommandName="Edit" OnCommand="Button1_Command" Text="Edit" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle HorizontalAlign="Center" Font-Bold="True" ForeColor="Black"></HeaderStyle>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
The problem with modals is that they usually place the contents outside of the <form> tag. And then the Buttons do not work anymore.
Replace the Buttons with LinkButtons. They perform a PostBack with JavaScript and will work from a Modal.
I wasnt handling rowediting event hence I was getting an error when I checked the developer view in firefox.Now it fires for me to edit but its after the second click of the link button before it enables the editing.Any ideas?

Is there a way to turn a Button in GridView to a link text?

The size of the button really kills the width of my GridView and so I am looking to just change the buttons into a link (like if you were to use AutoGenerateEditButton or AutoGenerateDeleteButton). Here's what I have:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="editButton" runat="server" CommandName="Edit"
Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button ID="BtnUpdate" runat="server" CommandName="Update"
Text="Edit" />
<asp:Button ID="BtnCancel" runat="server" CommandName="Cancel"
Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
How would I change what I have into just a text that is like a link (once again, like if it was from AutoGenerateEditButton). I can't set that to true because I need to conditionally hide it and I can't seem to do it if it's automatically generated. If you know a way to conditionally hide it when it's automatically generated, please let me know!
Take note that I'm using a SQLDataSource.
Use a LinkButton MSDN
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="editButton" runat="server" CommandName="Edit">Edit</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<div style="white-space: nowrap;">
<asp:LinkButton ID="BtnUpdate" runat="server" CommandName="Update">Edit</asp:LinkButton>
<asp:LinkButton ID="BtnCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</div>
</EditItemTemplate>
</asp:TemplateField>

Getting client ID using jquery

Im trying to validate a textbox by getting the client ID using the below and then testing its value
The textbox is in a ModalPopupExtender which is in a updatepanel
However JQuery doesnt seem to be returning anything, the control is always NULL
This textbox is inside a gridview
<asp:TemplateField HeaderText="Quick Donate">
<ItemTemplate>
<asp:Button ID="btnQuickDonate" runat="server"
CommandName="Insert"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
OnClick="btnQuickDonate"
Text="Quick Donate" />
<ajaxToolkit:ModalPopupExtender ID="ModalDonationPopup" runat="server" TargetControlID="btnQuickDonate" PopupControlID="Panel1" BackgroundCssClass="modalBackground">
</ajaxToolkit:ModalPopupExtender>
<asp:Panel ID="Panel1" runat="server" CssClass="modalPopup" Style="display: none">
Enter Donation Amount<br />
<table>
<tr>
<td></td>
<td>
<asp:Button ID="btnOnePound" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="1" />
<asp:Button ID="btnFivePound" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="5" />
<asp:Button ID="btnTenPound" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="10" />
<asp:Button ID="btnFifteenPound" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="15" />
<asp:Button ID="btnTwentyPounds" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="20" />
<asp:Button ID="btnTwentyFivePounds" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="25" />
<asp:Button ID="btnThirtyPounds" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="30" />
<asp:Button ID="btnFiftyPounds" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClick="btnQuickDonate" Text="50" />
</td>
</tr>
<%-- --%>
</table>
<br />
<asp:TextBox ID="txtFreeDonationAmount" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" ></asp:TextBox>
**<asp:Button ID="btnOK" runat="server" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" Text="OK" OnClientClick="return valtxtBoxFreeDonation();" OnClick="btnQuickDonate"></asp:Button>**
<br />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" />
</asp:Panel>
Javascript below I have also tried $('#txtFreeDonationAmount') with no joy...
function valtxtBoxFreeDonation() {
var txtDonation = $('#<%=txtFreeDonationAmount.ClientID %>');
if (txtDonation.val().length < 0) {
// do something
apprise("Please Enter a Donation Amount before click okay");
return false;
}
}
This code:
var txtDonation = $('#<%=txtFreeDonationAmount.ClientID %>');
will work properly only when JavaScript is inside .aspx file.
The bold solution is jsut to take this id 'GridView1_btnOK_1` and use it in your JS.
$('#GridView1_txtFreeDonationAmount_0')
And as mentiones about the $('#<%=txtFreeDonationAmount.ClientID %>') will work only if your JS is in the same .aspx file.

Conflicts between multiple gridviews

I have multiple gridviews on one page. For each gridview, there are two ways to insert a new record. One is through FooterTemplate of a TemplateField when the gridview has data and the other through EmptyDataTemplate when the gridview is empty.
By itself, the inserts work well. However, if another gridview is empty, then by default, the insert portion in its EmptyDataTemplate is displayed. When this is displayed, I can't get the FooterTemplate of another gridview to display. That portion is inaccessible until I get rid of the EmptyDataTemplate by forcing an insert.
<asp:Button ID="btnAddNewDomSoilType" runat="server" Text="Add new record" CssClass="btnNewRecord" OnClick="addNewDomSoilType" />
<asp:GridView ID="gvDomSoilType" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="dsDomSoilType" CssClass="data"
AllowSorting="True" onrowcommand="gvDomSoilType_RowCommand" DataKeyNames="HabitatObsSubstrateID, HabitatObservationID, SubstrateID" >
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"
OnClientClick="return confirm('Are you sure?');"
AlternateText="Delete" ForeColor="Blue"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnInsertSubmit" runat="server" Text="Submit" CommandName="SubmitInsert" CssClass="btnInsertSEID" />
<asp:Button ID="btnInsertCancel" runat="server" Text="Cancel" CommandName="CancelInsert" CssClass="btnInsertSEID" CausesValidation="false" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Substrate" SortExpression="Substrate">
<FooterTemplate>
<asp:DropDownList ID="ddlSoilType" runat="server"
DataSourceID="dsDomSoilType_Insert" DataTextField="Description"
DataValueField="Value" AppendDataBoundItems="true">
<asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Substrate") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:Label ID="Label4" runat="server" Text="Substrate: " CssClass="insertLabel"></asp:Label>
<asp:DropDownList ID="ddlSoilType2" runat="server"
DataSourceID="dsDomSoilType_Insert" DataTextField="Description"
DataValueField="Value" AppendDataBoundItems="true">
<asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnInsertEmpty" runat="Server" Text="Insert" CommandName="EmptyInsert" UseSubmitBehavior="False" />
</EmptyDataTemplate>
</asp:GridView>
<asp:Button ID="btnAddNewVegCover" runat="server" Text="Add new record" CssClass="btnNewRecord" OnClick="addNewVegCover" />
<asp:GridView ID="gvVegCover" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataSourceID="dsVegCover" CssClass="data"
AllowSorting="True" onrowcommand="gvVegCover_RowCommand" DataKeyNames="HabitatObsVegID">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete"
OnClientClick="return confirm('Are you sure?');"
AlternateText="Delete" ForeColor="Blue"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnInsertSubmit" runat="server" Text="Submit" CommandName="SubmitInsert" CssClass="btnInsertSEID" />
<asp:Button ID="btnInsertCancel" runat="server" Text="Cancel" CommandName="CancelInsert" CssClass="btnInsertSEID" CausesValidation="false" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="HabitatVegetationType" SortExpression="HabitatVegetationType">
<FooterTemplate>
<asp:DropDownList ID="ddlVegType" runat="server"
DataSourceID="dsVegType_Insert" DataTextField="Description"
DataValueField="Value" AppendDataBoundItems="true">
<asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblVegType" runat="server" Text='<%# Bind("HabitatVegetationType") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="VegetationTypePercent" SortExpression="VegetationTypePercent">
<FooterTemplate>
<asp:TextBox ID="txtVegTypePercent" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblVegTypePercentage" runat="server" Text='<%# Bind("VegetationTypePercent") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:Label ID="Label2" runat="server" Text="Vegetation Type:" CssClass="insertLabel"></asp:Label>
<asp:DropDownList ID="ddlVegType2" runat="server"
DataSourceID="dsVegType_Insert" DataTextField="Description"
DataValueField="Value" AppendDataBoundItems="true">
<asp:ListItem Text="" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label5" runat="server" Text="Percenttage:" CssClass="insertLabel"></asp:Label>
<asp:TextBox ID="txtVegTypePercent2" runat="server"></asp:TextBox>
<asp:Button ID="btnInsertEmpty" runat="Server" Text="Insert" CommandName="EmptyInsert" UseSubmitBehavior="False" />
</EmptyDataTemplate>
</asp:GridView>
Code behind:
protected void addNewDomSoilType(object sender, EventArgs e)
{
gvDomSoilType.ShowFooter = true;
}
protected void addNewVegCover(object sender, EventArgs e)
{
gvVegCover.ShowFooter = true;
}
I know my explanation is a bit confusing. Let me know what doesn't make sense and I'll try to clarify. Thanks for looking.
These two gridviews shouldn't impact each other. I think it might be the PostBack that is messing up the state of the webpage. On the PageLoad do Page.IsPostBack false and then populate the data. Make sure the events are hooked up to the right GridView.

Exception handling in WebForms

I have a webform with a formview
<asp:FormView ID="formViewBrouwers" runat="server" AllowPaging="True"
DataKeyNames="BrouwerNr" DataSourceID="brouwerDataSource"
onitemupdated="formViewBrouwers_ItemUpdated"
onitemupdating="formViewBrouwers_ItemUpdating"
oniteminserted="formViewBrouwers_ItemInserted"
oniteminserting="formViewBrouwers_ItemInserting">
<EditItemTemplate>
BrouwerNr:
<asp:Label ID="BrouwerNrLabel1" runat="server"
Text='<%# Eval("BrouwerNr") %>' />
<br />
BrNaam:
<asp:TextBox ID="BrNaamTextBox" runat="server" Text='<%# Bind("BrNaam") %>' />
<br />
Adres:
<asp:TextBox ID="AdresTextBox" runat="server" Text='<%# Bind("Adres") %>' />
<br />
Postcode:
<asp:TextBox ID="PostcodeTextBox" runat="server"
Text='<%# Bind("Postcode") %>' />
<br />
Gemeente:
<asp:TextBox ID="GemeenteTextBox" runat="server"
Text='<%# Bind("Gemeente") %>' />
<br />
Omzet:
<asp:TextBox ID="OmzetTextBox" runat="server" Text='<%# Bind("Omzet") %>' />
<br />
Status:
<asp:TextBox ID="StatusTextBox" runat="server" Text='<%# Bind("Status") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
<InsertItemTemplate>
BrNaam:
<asp:TextBox ID="BrNaamTextBox" runat="server" Text='<%# Bind("BrNaam") %>' />
<br />
Adres:
<asp:TextBox ID="AdresTextBox" runat="server" Text='<%# Bind("Adres") %>' />
<br />
Postcode:
<asp:TextBox ID="PostcodeTextBox" runat="server"
Text='<%# Bind("Postcode") %>' />
<br />
Gemeente:
<asp:TextBox ID="GemeenteTextBox" runat="server"
Text='<%# Bind("Gemeente") %>' />
<br />
Omzet:
<asp:TextBox ID="OmzetTextBox" runat="server" Text='<%# Bind("Omzet") %>' />
<br />
Status:
<asp:TextBox ID="StatusTextBox" runat="server" Text='<%# Bind("Status") %>' />
<br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
<asp:LinkButton ID="InsertCancelButton" runat="server"
CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</InsertItemTemplate>
<ItemTemplate>
BrouwerNr:
<asp:Label ID="BrouwerNrLabel" runat="server" Text='<%# Eval("BrouwerNr") %>' />
<br />
BrNaam:
<asp:Label ID="BrNaamLabel" runat="server" Text='<%# Bind("BrNaam") %>' />
<br />
Adres:
<asp:Label ID="AdresLabel" runat="server" Text='<%# Bind("Adres") %>' />
<br />
Postcode:
<asp:Label ID="PostcodeLabel" runat="server" Text='<%# Bind("Postcode") %>' />
<br />
Gemeente:
<asp:Label ID="GemeenteLabel" runat="server" Text='<%# Bind("Gemeente") %>' />
<br />
Omzet:
<asp:Label ID="OmzetLabel" runat="server" Text='<%# Bind("Omzet") %>' />
<br />
Status:
<asp:Label ID="StatusLabel" runat="server" Text='<%# Bind("Status") %>' />
<br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
CommandName="Edit" Text="Edit" />
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False"
CommandName="Delete" Text="Delete" />
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False"
CommandName="New" Text="New" />
</ItemTemplate>
<PagerSettings Mode="NextPreviousFirstLast" />
</asp:FormView>
In my property Postcode I check the value like this:
private Int16 postcodeValue;
public Int16 Postcode
{
get
{
return postcodeValue;
}
set
{
if (value < 1000 || value > 9999)
{
throw new Exception("Postcode moet tussen 1000 en 9999 liggen");
}
else
{
postcodeValue = value;
}
}
}
How can I handle the exception I threw? If there is an exception I want a label to appear with the following exception?
You can associate the PostCode textbox in InsertItemTemplate and EditItemTemplate with a RangeValidator and have a label beside that shows the error message when the condition fails.
Maybe you should use a derived exception type, like ArgumentException or a custom class, something like PostCodeException.
This way you can catch the correct exception type and react accordingly.
This article may be useful: http://msdn.microsoft.com/en-us/library/ms173160(v=VS.100).aspx
You could use a RegularExpressionValidator or RangeValidator that would validate the value on the client side before submitting it to the server.
This '^[1-9]{1}[0-9]{3} ?[A-Z]{2}$' would be a valid regex to check a Dutch postal code.
Or instead of throwing an exception in your property setter, you could show a label that is normally hidden in your markup.
If you really want to throw an exception have a look at the Page_Error event. You can use that to catch all exceptions in your page and handle them if appropriately.

Categories

Resources