DataSource ItemInserted Populating Datbound TextBox

Last post 05-31-2008 8:00 PM by ionafi. 6 replies.

Sort Posts:

  • DataSource ItemInserted Populating Datbound TextBox

    05-16-2008, 9:42 PM
    • Loading...
    • grahowler
    • Joined on 01-17-2008, 1:32 AM
    • Posts 18

    Hi

    I can't believe I am having trouble with this, but I can't get a TextBox to have a value inside a DataSource_ItemInserted event. Here's what's happening...

    bool MyBool = false;
    
            // There is some custom validation that is actually being done
            // in a stored procedure, which will return false, but this
            // has the  same affect.
            if (!MyBool)
            {
                // The way I'm getting the submitted value
                // then trying to set the FormView control 
                // is ugly but I'm not sure how else to do it
                string TextBoxValue = Request.Form[((TextBox)FormView1.FindControl("MyTextBox")).UniqueID].ToString();
                
                ((TextBox)FormView1.FindControl("MyTextBox")).Text = TextBoxValue;
            }

     So I am trying to set a textbox to the value that was entered before the page was posted, and when a insert procedure has failed.

    Hopefully I've made that clear, any ideas?

    Thanks!

  • Re: DataSource ItemInserted Populating Datbound TextBox

    05-17-2008, 2:43 AM
    • Loading...
    • vinkesh
    • Joined on 05-15-2008, 2:34 AM
    • Posts 41

    Hi,

                  I am not getting idea about your problem. Can you please submit code and design for better idea.

     

     

    Mark this post as "Answered" if it helps

    Thank You.
  • Re: DataSource ItemInserted Populating Datbound TextBox

    05-18-2008, 9:13 PM
    • Loading...
    • grahowler
    • Joined on 01-17-2008, 1:32 AM
    • Posts 18

    Hi, thanks for the response.

    I have a storedprocedure as follows...

    ALTER PROCEDURE [dbo].[MyProc]
    (
    	@ID int = NULL OUTPUT,
    	@Name varchar(50)
    )
    AS
    	SET NOCOUNT ON
    
    	DECLARE @Result int
    	
    	BEGIN TRANSACTION
    
    	IF EXISTS
    	(
    		SELECT
    			1
    		FROM [MyTable] WITH (UPDLOCK)		
    		WHERE [Name] = @Name
    	)
    		BEGIN
    			SELECT @Result = -1
    		END
    	ELSE
    		BEGIN
    			INSERT INTO [MyTable]
    			(
    				[Name]
    			)
    			VALUES
    			(
    				@Name
    			)
    		
    			SELECT @ID = SCOPE_IDENTITY();
    		END
    		
    	IF @Result <> 0
    		BEGIN
    			
    			ROLLBACK
    		END
    	ELSE
    		BEGIN
    			COMMIT
    		END
    	RETURN @Result
    

     

    If the field "Name" already exists in the database it will return -1. Then I am running the following code...

    1        protected void MyDataSource_Inserted(object sender, SqlDataSourceStatusEventArgs e)
    2        {
    3            if ((Int32)e.Command.Parameters["@Result"].Value == -1)
    4            {
    5                Label1.Text = "Record Exists.";
    6                // Populate submitted fields with relevant values
    7            }
    8            else
    9            {
    10               Label1.Text = "Insert Complete.";
    11           }
    12       }

    So at line six I am trying to populate the previously submitted with the relevant values. That's where you insert the code from my previous post, replaces the MyBool with line 3 from above.

    Does that help?

    Thanks again.

  • Re: DataSource ItemInserted Populating Datbound TextBox

    05-19-2008, 2:48 AM
    Answer
    • Loading...
    • vinkesh
    • Joined on 05-15-2008, 2:34 AM
    • Posts 41

    Hi, 

               Set your FormView's EnableViewState property to True and if record exist than again set formview's mode to insert as below

     FormView1.ChangeMode(FormViewMode.Insert)

     

     

    Mark this post as "Answered" if it helps.

    Mark this post as "Answered" if it helps

    Thank You.
  • Re: DataSource ItemInserted Populating Datbound TextBox

    05-19-2008, 12:25 PM
    Answer

    Hi grahowler,

    After the DataSource control's inserted event fired, the FormView will go to normal mode instead of insert mode. So I think MyTextBox control will not show in that mode. You'd better leave the FormView control in insert mode.

    see my sample,

     

           protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
            {
                e.KeepInInsertMode = true;
            }
    
            protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
            {
                
                //TextBox box =  this.FormView1.FindControl("countryidTextBox") as TextBox;
                //box.Text = this.Request.Form[box.UniqueID].ToString();
            }
    
            protected void FormView1_ModeChanging(object sender, FormViewModeEventArgs e)
            {
                //if (this.FormView1.CurrentMode == FormViewMode.Insert)
                //{
                //    TextBox box = this.FormView1.FindControl("countryidTextBox") as TextBox;
                //    box.Text = this.Request.Form[box.UniqueID].ToString();
                //}
            }
    
            protected void FormView1_PreRender(object sender, EventArgs e)
            {
                if (this.FormView1.CurrentMode == FormViewMode.Insert)
                {
                    TextBox box = this.FormView1.FindControl("countryidTextBox") as TextBox;
                    if (this.Request.Form[box.UniqueID] != null)
                    {
                        box.Text = this.Request.Form[box.UniqueID].ToString();
                    }
                }
            }
       
        <form id="form1" runat="server">
        <div>
        
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
                DeleteCommand="DELETE FROM [country] WHERE [countryid] = @countryid" 
                InsertCommand="INSERT INTO [country] ([countryid], [countryname]) VALUES (@countryid, @countryname)" 
                oninserted="SqlDataSource1_Inserted" SelectCommand="SELECT * FROM [country]" 
                UpdateCommand="UPDATE [country] SET [countryname] = @countryname WHERE [countryid] = @countryid">
                <DeleteParameters>
                    <asp:Parameter Name="countryid" Type="Int64" />
                </DeleteParameters>
                <UpdateParameters>
                    <asp:Parameter Name="countryname" Type="String" />
                    <asp:Parameter Name="countryid" Type="Int64" />
                </UpdateParameters>
                <InsertParameters>
                    <asp:Parameter Name="countryid" Type="Int64" />
                    <asp:Parameter Name="countryname" Type="String" />
                </InsertParameters>
            </asp:SqlDataSource>
            <asp:FormView ID="FormView1" runat="server" DataKeyNames="countryid" 
                DataSourceID="SqlDataSource1" oniteminserted="FormView1_ItemInserted" 
                onmodechanging="FormView1_ModeChanging" onprerender="FormView1_PreRender">
                <EditItemTemplate>
                    countryid:
                    <asp:Label ID="countryidLabel1" runat="server" 
                        Text='<%# Eval("countryid") %>' />
                    <br />
                    countryname:
                    <asp:TextBox ID="countrynameTextBox" runat="server" 
                        Text='<%# Bind("countryname") %>' />
                    <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>
                    countryid:
                    <asp:TextBox ID="countryidTextBox" runat="server" 
                        Text='<%# Bind("countryid") %>' />
                    <br />
                    countryname:
                    <asp:TextBox ID="countrynameTextBox" runat="server" 
                        Text='<%# Bind("countryname") %>' />
                    <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>
                    countryid:
                    <asp:Label ID="countryidLabel" runat="server" Text='<%# Eval("countryid") %>' />
                    <br />
                    countryname:
                    <asp:Label ID="countrynameLabel" runat="server" 
                        Text='<%# Bind("countryname") %>' />
                    <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>
            </asp:FormView>
        
        </div>
        </form>
     
    Sincerely,
    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
  • Re: DataSource ItemInserted Populating Datbound TextBox

    05-19-2008, 5:36 PM
    • Loading...
    • grahowler
    • Joined on 01-17-2008, 1:32 AM
    • Posts 18

     

            protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
            {
                e.KeepInInsertMode = true;
            }
    

     

    Perfect, thanks.

  • Re: DataSource ItemInserted Populating Datbound TextBox

    05-31-2008, 8:00 PM
    • Loading...
    • ionafi
    • Joined on 10-06-2007, 3:25 PM
    • Posts 71

    Hi there,

    I've been struggling with this for a while. Same problem same solution.

    It's not clear for me though: after inserting, the formview doesn't go back to the default mode? Because my default mode was insert, the formview was showing the insert template after inserting but still i couldn't populate the textboxes within it. Why??

     

     

Page 1 of 1 (7 items)
Microsoft Communities
Page view counter