I'm pretty lost at this point (been working at it for a while not and am hitting a wall / deadline) but the error message I am being thrown is after I hit btnupdate to update the fields in the database.
Full Error Message:
Could not find control 'txtTitle' in ControlParameter 'Title'.
Page:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div class="floater">
<h3>Books</h3>
<asp:DropDownList
id="DropDownList_Books"
DataSourceId="srcBooks"
DataTextField="Title"
DataValueField="Id"
Runat="server" />
<asp:Button ID="btnSelect" runat="server" Text="View Detials" Width="106px"
onclick="btnSelect_Click" />
</div>
<asp:GridView
id="grdBooks"
DataSourceID="srcBooks_Description"
Runat="server" Visible="False" />
<asp:Button ID="btnEdit" runat="server" onclick="btnEdit_Click" Text="Edit" />
</div>
<asp:Button ID="btnCancel" runat="server" onclick="btnCancel_Click"
Text="Cancel" Visible="False" />
<asp:FormView
id="frmEditBook"
DataKeyNames="Cat_Id"
DataSourceId="srcBooks_Description"
DefaultMode="Edit"
Runat="server" Visible="False"
style="z-index: 1; left: 391px; top: 87px; position: absolute; height: 111px; width: 206px" >
<EditItemTemplate>
<asp:Label
id="lblTitle"
Text="Title:"
AssociatedControlID="txtTitle"
Runat="server" />
<asp:TextBox
id="txtTitle"
Text='<%#Bind("Title")%>'
Runat="server" />
<br />
<asp:Label
id="lblDescription"
Text="Description:"
AssociatedControlID="txtDescription"
Runat="server" />
<asp:TextBox
id="txtDescription"
Text='<%#Bind("Description")%>'
Runat="server" />
<br />
<asp:Button
id="btnUpdate"
Text="Update"
CommandName="Update"
Runat="server" />
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="srcBooks" runat="server"
ConnectionString="Data Source=****;;Initial Catalog=***;Persist Security Info=True;User ID=****;Password=****"
onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT Title,Id FROM PARTIN_ID">
</asp:SqlDataSource>
<asp:SqlDataSource ID="srcBooks_Description" runat="server"
ConnectionString="Data Source=****, 14330";Initial Catalog=****;Persist Security Info=True;User ID=****;Password=****"
onselecting="srcBooks_Selecting" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM PARTIN_INFO WHERE Cat_ID=#Id" UpdateCommand="UPDATE PARTIN_INFO SET Title=#Title,
Description=#Description WHERE Cat_Id=#Id">
<SelectParameters>
<asp:ControlParameter
Name="Id"
Type="int32"
ControlID="DropDownList_Books"
PropertyName="SelectedValue" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="Title" ControlId="txtTitle" PropertyName="Text"/>
<asp:ControlParameter Name="Description" ControlId="txtDescription" PropertyName="Text"/>
<asp:ControlParameter Name="Id" ControlId="DropDownList_Books" PropertyName="SelectedValue"/>
</UpdateParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
Code Behind:
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void srcBooks_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
protected void btnSelect_Click(object sender, EventArgs e)
{
grdBooks.Visible = true;
}
protected void btnCancel_Click(object sender, EventArgs e)
{
frmEditBook.Visible = false;
}
protected void btnEdit_Click(object sender, EventArgs e)
{
frmEditBook.Visible = true;
btnCancel.Visible = true;
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
srcBooks_Description.Update();
}
catch (Exception except)
{
// Handle the Exception.
}
}
}
}
You -can- refer to the control adequately.
You need to prefix the controlid value of your update parameters with the control id of your enclosing view (FormView 'frmEditBook' in this case)
Replace the UpdateParamters section of your SqlDataSource with the following:
<UpdateParameters>
<asp:ControlParameter Name="Title" ControlId="frmEditBook$txtTitle" PropertyName="Text"/>
<asp:ControlParameter Name="Description" ControlId="frmEditBook$txtDescription" PropertyName="Text"/>
<asp:ControlParameter Name="Id" ControlId="DropDownList_Books" PropertyName="SelectedValue"/>
</UpdateParameters>
Note that
ControlId="DropDownList_Books"
remains the same as this control is not within the bounds of the FormView.
I don't recommend using the 'full name' provided by the browser... particularly if you're using master pages... it could change as you rearrange your layouts.
As you can see, the problem is the context. You can also put your sqldatasource inside the formview where the control being refered to is (not properly and very limited but could work)
Let's say you have a detailsview with two dropdowns in it, and one is dependant from another. What works for me is to put the Datasource in the same context (parent control) as the control refered and this will eliminate the need to reference the full path of the control.
My two cents is that this is very reminiscent on how you must reference controls in Access Forms with subforms. :)
<asp:DetailsView runat="server">
<asp:TemplateField>
<EditTemplate>
<asp:DropDownList id="ParentDDL" Datasource="SQLDataSource1">
</asp:DropDownList>
</EditTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditTemplate>
<asp:DropDownList id="ParentDDL" Datasource="SQLDatasource2">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT * from table where field1=#field">
<SelectParameters>
<asp:ControlParameter ControlID="ParentDDL" Name="field"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</EditTemplate>
</asp:TemplateField>
</asp:DetailsView>
You need to refer to your control with its full name.
You can easily find the control's full name when you open the page in the browser. Simply view source and you will see it.
The problem is that txtTitle is inside a FormView so it's not accesible in the way you're trying to do on the SqlDataSource. You can't access it direclty by ID.
Here you have an MSDN article that may help to to work with a FormView and SqlDataSource
you shouldn't have used <asp:ControlParameter.. but <asp:Parameter.. and the SqlDataSource control with the help of the bind method is intelligent enough to locate the controls to get values from for the parameters.
Update this portion of the sqlDataSource this way:
....
<UpdateParameters>
<asp:Parameter Name="Title" Type="String"/>
<asp:Parameter Name="Description" Type="String"/>
<asp:Parameter Name="Id" Type="Int32"/>
</UpdateParameters>
.....
Please note the Type attribute and supply the relevant type.
(The problem is SqlDataSource control cannot explicitly access controls defined in a databound control)
Related
I want to modify items in gridview by update command but in same page i have also a textbox with required validation.
i'm unable to update in gridview when require validation occur in textbox Control
And Source Code here...
<div class="row">
<asp:Button ID="Button1" runat="server" CssClass="btn btn-primary" Text="Add" OnClick="Button1_Click"
ValidationGroup="btn" />
<asp:Button ID="Button2" runat="server" CssClass="btn btn-primary" Text="Reset" OnClick="Button2_Click"
CausesValidation="False" />
<asp:Button ID="Button3" runat="server" CssClass="btn btn-primary" Text="Show" CausesValidation="False"
OnClick="Button3_Click" />
</div>
</form> </div> </div>
<asp:GridView ID="GridView1" runat="server" CssClass="table-hover table-responsive table-condensed table-bordered"
AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="id"
DataSourceID="SqlDataSource1" Visible="False">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="Loc" HeaderText="Location" SortExpression="Loc" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mycon %>"
SelectCommand="SELECT * FROM [location]" DeleteCommand="Delete From Location Where Id=#id"
UpdateCommand="update location set loc=#loc where id=#id">
<DeleteParameters>
<asp:ControlParameter ControlID="TextBox1" Name="id" PropertyName="Text" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="loc" />
<asp:ControlParameter ControlID="GridView1" Name="id" PropertyName="SelectedValue" />
</UpdateParameters>
</asp:SqlDataSource>
and cs code here
protected void Button1_Click(object sender, EventArgs e)
{
string qry = "insert into location (loc)values(#loc) ";
SqlConnection con = Connection.Getconnection();
con.Open();
SqlCommand cmd = new SqlCommand(qry, con);
cmd.Parameters.AddWithValue("#loc", TextBox1.Text);
int x = cmd.ExecuteNonQuery();
if (x > 0)
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation!", "<script>alert('Successfully Add')</script>");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation!", "<script>alert('Error')</script>");
}
}
protected void Button2_Click(object sender, EventArgs e)
{
TextBox1.Text = string.Empty;
}
protected void Button3_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
}
Please try to add try catch for button1_click to catch Exceptions.
Validation group has an effect only when the value of the CausesValidation property is set to true.and validation group need to specify a value
Please Refer this Link
I have a pretty standard GridView with an SqlDataSource complete with SelectCommand/UpdateCommand/DeleteCommand.
I am utilising the Gridview's built-in commands for TemplateField'ed 's.
CommandName="Update"
CommandName="Edit"
CommandName="Cancel"
CommandName="Delete"
Everything works OK until I put the GridView inside an UpdatePanel.
1. When I click on 'edit' it goes into edit mode but won't get out of edit mode when you click Cancel or Update.
2. When I click on 'delete' performs as it should, however, I won't be able to get into edit mode anymore even if I click 'edit'
What's the matter here?
Example code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="BoxFloat">
<h4>Aliases</h4>
<hr />
<asp:SqlDataSource ID="sqlPersonAlias" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
ProviderName="<%$ ConnectionStrings:myConnectionString.ProviderName %>"
DeleteCommand="DELETE FROM PersonAlias WHERE PersonAliasId=?PersonAliasId"
UpdateCommand="UPDATE PersonAlias SET AliasName=?AliasName WHERE PersonAliasId=?PersonAliasId"
SelectCommand="SELECT * FROM PersonAlias E INNER JOIN Person P ON P.PersonId=E.PersonId WHERE Username=?Username">
<DeleteParameters>
<asp:Parameter Name="PersonAliasId" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="PersonAliasId" />
<asp:Parameter Name="AliasName" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter Name="Username" QueryStringField="Username" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="gridviewPersonAlias" runat="server"
AutoGenerateColumns="False"
DataKeyNames="PersonAliasId"
DataSourceID="sqlPersonAlias">
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:ImageButton ID="imagebuttonCancel" ToolTip="Cancel" CommandName="Cancel" ImageUrl="~/images/cancel16.png" runat="server" />
<asp:ImageButton ID="imagebuttonUpdate" ToolTip="Apply" OnClientClick="return confirm('Are you sure?');" CommandName="Update" ImageUrl="~/images/apply16.png" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="imagebuttonDelete" ToolTip="Delete" OnClientClick="return confirm('Are you sure?');" CommandName="Delete" ImageUrl="~/images/delete16.png" runat="server" />
<asp:ImageButton ID="imagebuttonEdit" ToolTip="Edit" CommandName="Edit" ImageUrl="~/images/edit16.png" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="AliasName" HeaderText="Alias" SortExpression="AliasName" />
</Columns>
</asp:GridView>
<hr />
<asp:TextBox ID="textboxPersonAlias" runat="server" />
<asp:Button ID="buttonPersonAliasAdd" runat="server" Text="Add" onclick="buttonPersonAliasAdd_Click" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
When "Update" is clicked, the database gets updated. It seems that the problem is just with the client-side.
Note: I'm using "?" instead of "#" for the parameters because that SqlDataSource is using MySq instead of MSSQL (I love VS2010).
I think what you need is full Postback or refresh the update Panel since it is most likely the gridview doesnt rebind
First try to put the sqldatasource outside the update panel. See if that works
I had the same problem in my project, and solved it.
here is my solution:
protected void gvPayments_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//here is the edit code
//......
//at the end add these lines:
gvPayments.EditIndex = -1;
gvPayments.DataBind();
e.Cancel = true;
}
I have a main 'Products' table and a 'Products_Recommended' table. I want the users to be able to select several products from a GridView using checkboxes and then insert those Product IDs (prodid) into the Products_Recommended table in such a way that a main Product ID is entered (coming from query string) and potentially several recommended ProdIDs get entered. So far so good.
But I need to be able to show the checkboxes to be checked if there were already prodids in the Products_Recommended table previously.
The code below show a 'sqldatasource1' which gets the data from the Products_Recommended table based on query string. I just don't know how the checkboxes can get checked because the GridView has a different sqldatasource binding it.
Thanks!
Meengla
<form id="form1" runat="server">
<asp:GridView ID="Products" runat="server" AutoGenerateColumns="False" DataKeyNames="prodid"
DataSourceID="alldata" EnableModelValidation="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ProductSelector" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="itemnumber" HeaderText="Item Number" SortExpression="itemnumber" />
<asp:BoundField DataField="itemtitle" HeaderText="itemtitle" SortExpression="itemtitle" />
</Columns>
</asp:GridView>
<p>
<asp:Button ID="SelectedProducts" runat="server" Text="Recommend" OnClick="SelectedProducts_Click" />
</p>
<p>
<asp:Label ID="lblProdSelected" runat="server" EnableViewState="False" Visible="False"></asp:Label>
</p>
<asp:SqlDataSource ID="alldata" runat="server" ConnectionString="<%$ ConnectionStrings:dbconnection %>"
SelectCommand="SELECT * FROM Products">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="14" Name="itemid" QueryStringField="itemid"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbconnection %>"
SelectCommand="SELECT * FROM dbo.products_recommended WHERE prodid = #itemid)">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="14" Name="itemid" QueryStringField="itemid"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Handle the RowDataBound event, and in the method find the checkbox and set its Checked value.
<asp:GridView ID="Products" OnRowDataBound="GridViewRowEventHandler">
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var ProductSelector = e.Row.FindControl("ProductSelector") as CheckBox;
ProductSelector.Checked = true;
}
}
You can use the Select method of DataSource to retrieve the data you need
I am trying to nest a grid within a grid.
To populate the most inner grid i need the KeyValue from both the Grids' direct parent, and the parent of the Grids' parent.
How can i retrieve the MasterRowKeyValue from the outter most parent?
I have 3 grids nested like so:
<dxwgv:ASPxGridView ID="gridParents" runat="server" ObjectDataSource="odsParents" KeyFieldName="ParentId">
<SettingsDetail ShowDetailRow="true" />
<Columns>
<dx:GridViewDataColumn FieldName="ParentIdId" Visible="false" />
<dx:GridViewDataColumn FieldName="Title" />
</Columns>
<Templates>
<DetailRow>
<dxwgv:ASPxGridView ID="gridParentsChilren" runat="server" ObjectDataSource="odsParentsChildren" KeyFieldName="ChildId" OnBeforePerformDataSelect="gridParentsChilren_DataSelect">
<SettingsDetail ShowDetailRow="true" IsDetailGrid="true" />
<Columns>
<dx:GridViewDataColumn FieldName="ChildId" Visible="false" />
<dx:GridViewDataColumn FieldName="Title" />
</Columns>
<Templates>
<dxwgv:ASPxGridView ID="gridParentsChilrenRoles" runat="server" ObjectDataSource="odsParentsChildrenRoles" KeyFieldName="RoleId" OnBeforePerformDataSelect="gridParentsChilrenRoles_DataSelect">
<SettingsDetail ShowDetailRow="true" IsDetailGrid="true" />
<Columns>
<dx:GridViewDataColumn FieldName="RoleId" Visible="false" />
<dx:GridViewDataColumn FieldName="Title" />
</Columns>
</dxwgv:ASPxGridView>
</Templates>
</dxwgv:ASPxGridView>
</DetailRow>
</Templates>
</dxwgv:ASPxGridView>
<asp:ObjectDataSource id="odsParents" runat="server" SelectMethod="GetParents" />
<asp:ObjectDataSource id="odsParentsChildren" runat="server" SelectMethod="GetParentsChildren">
<SelectParameters>
<asp:Parameter Name="parentid" Type="Object" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource id="odsParentsChildrenRoles" runat="server" SelectMethod="GetParentsRoles">
<SelectParameters>
<asp:Parameter Name="childid" Type="Object" />
<asp:Parameter Name="parentid" Type="Object" />
</SelectParameters>
</asp:ObjectDataSource>
When the OnBeforePerformDataSelect is called for the first Detail grid, i set the select parameters DefaultValue like this:
odsParentsChildren.SelectParameters["parentid"].DefaultValue = ((ASPxGridView)sender).GetMasterRowKeyValue().ToString();
For the most inner (the second) nested grid, i need to set one of my parameters to the MasterRowKeyValue of the most outter grid.
Something like:
odsParentsChildrenRoles.SelectParameters["childid"].DefaultValue = ((ASPxGridView)sender).GetMasterRowKeyValue().ToString();
odsParentsChildrenRoles.SelectParameters["parentid"].DefaultValue = ((ASPxGridView)sender).Master.GetMasterRowKeyValue().ToString();
How do I achieve this?
This can be done if you determine the master ASPxGridView instance for the processed detail. This can be done using the following code:
protected void ASPxGridView1_BeforePerformDataSelect(object sender, EventArgs e) {
ASPxGridView subDetail = sender as ASPxGridView;
GridViewDetailRowTemplateContainer container = subDetail.NamingContainer as GridViewDetailRowTemplateContainer;
ASPxGridView detail = container.Grid;
odsParentsChildrenRoles.SelectParameters["parentid"].DefaultValue = detail.GetMasterRowKeyValue();
}
Another possible version
protected void gvMyGridView_BeforePerformDataSelect(object sender, EventArgs e)
{
var gvMyGridView = sender as ASPxGridView;
var parentRow = gvMyGridView.NamingContainer as GridViewDetailRowTemplateContainer;
odsChildDataSource.SelectParameters["Id"].DefaultValue = parentRow .KeyValue.ToString();
}
Using Visual Web Developer Express 2010 with ASP.NET 4.0.
I have a FormView and I want to set a default value from the database. I can't get it to bind to the value in the database. My FormView looks like this:
<asp:FormView
ID="frmOrderDetails"
DataSourceID="sdsFormOrderDetails"
runat="server"
DataKeyNames="orderId">
<EditItemTemplate>
<h3>Edit Order Details</h3>
<asp:Label ID="lblStrategy" Text="Strategy:" AssociatedControlID="ddlStrategies" runat="server" />
<asp:DropDownList SelectedValue='<%# Bind("strategyId") %>'
ID="ddlStrategies"
runat="server"
DataTextField="strategy"
DataValueField="strategyId"
DataSourceID="sdsStrategies"
/>
<asp:LinkButton
id="lnkUpdate"
Text="Update Order"
CommandName="Update"
Runat="server" />
|
<asp:LinkButton
id="lnkCancel"
Text="Cancel"
CommandName="Cancel"
Runat="server" />
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="sdsFormOrderDetails" runat="server"
ConnectionString="<%$ ConnectionStrings:LocalSQLServer %>"
ProviderName="<%$ ConnectionStrings:LocalSQLServer.ProviderName %>"
SelectCommand="usp_GetOrderDetails" SelectCommandType="StoredProcedure"
UpdateCommand="usp_UpdateOrder" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter Name="orderId" ControlID="grdOrders" PropertyName="SelectedDataKey.Value" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="orderId" ControlID="grdOrders" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sdsStrategies" runat="server"
ConnectionString="<%$ ConnectionStrings:LocalSQLServer %>"
ProviderName="<%$ ConnectionStrings:LocalSQLServer.ProviderName %>"
SelectCommand="usp_GetStrategiesDropDown">
</asp:SqlDataSource>
The EditItemTemplate does not even load when I click the edit button on my FormView control, but I don't get an error message either.
You need to do the following to bind a DropDownList to the EditItemTemplate:
Add the DropDownList and its DataSource to the EditItemTemplate.
Set the DropDownList parameters:
DataSourceID="SqlDataSourceDropDownlist" SelectedValue=<%# Bind("ValueToBind") %>
DataTextField="ValueToDisplay" DataValueField="ValueToBind"
Set the ListView / FormView DataSource <UdateParameters>: <asp:Parameter Name="RankID" Type="Int32" />
you need to use formview Databound event like
protected void frmOrderDetails_DataBound(object sender, EventArgs e)
{
if (frmOrderDetails.CurrentMode == FormViewMode.Edit)
{
DropDownList ddlStrategies = (DropDownList)frmOrderDetails.FindControl("ddlStrategies");
ddlStrategies.SelectedValue = Your DB Value Goes here;
}
}